CS105 Study Guide

Unit 10: Object-Oriented Programming (OOP)

10a. Explain the differences between procedural, structured, and object-oriented programming

  • What is procedural programming?
  • How does OOP differ from procedural programming?
  • What is structured programming?

When it comes to programming techniques, it is best to arrange instructions into groups that reflect the desired computation. Arranging code so that it is modular is fundamental to structured programming. In this way, a program is broken down into a series of small modular steps, each of which solves a specific aspect of a larger problem. When these small modular steps and tasks are phrased in the form of functions and procedures, this is known as procedural programming.

Object-oriented programming differs from procedural programming. Instead of having multiple functions defined without regard to their inherent relationships, OOP arranges and groups together related data attributes and methods into a class. OOP takes a more global view when creating a program to solve a problem. A problem is first analyzed to determine how data and methods should be related. From this analysis, classes can be formed and the data attributes and methods can be categorized within each class definition.

To review, see Structured Programming and Procedural Programming and Procedural Programming versus Object-Oriented Programming.

 

10b. Explain how classes, objects, and instances are used in object-oriented programming

  • What information does a class definition contain?
  • How are instances, objects, and classes related?
  • How do the fundamental components of a class relate to objects?

A class definition in Python contains data attributes and methods that are related to the class. An instance of a class is known as an object. Creating an instance of a class is known as ‘instantiation’. Multiple objects of a given class can be instantiated. Each object keeps track of its own data attributes defined by the class. In addition, each object can use the methods defined within its class to operate on data.

An object can call a method from its own class. In order to do this, the dot notation must be used. In addition, input data to a class method can be used to modify a data attribute associated with a specific object. Therefore, OOP allows for each object to carry around its own data and access class methods to operate on and perform computations with that data. http://greenteapress.com/thinkpython2/html/thinkpython2018.html

To review, see Basic Terminology, Creating Classes and Methods, and Going Deeper.

 

10c. Implement simple programs that use classes, objects, and instances

  • What does the __init__ method do?
  • Explain how self is used.
  • What is a magic method and how are they useful for OOP in Python?

When an object is instantiated, if the __init__ method is defined within the class definition, it is automatically called. The __init__ method is generally used to initialize an object’s data attributes. More sophisticated uses could involve including exception handlers and memory management modules. When any method call takes place, by Python convention, the method makes reference to the associated object using self. The first argument in a method definition that modifies attribute data must be self. Therefore, the number of input parameters to a class method definition will differ from the method call by one parameter because self is implied and is not needed in the object’s method call.

Magic methods (also called dunder methods for ‘double underscore’) give the programmer an immense amount of power. This is because they can be used to generalize Python built-in operators and functions typically applied to built-in data types. For example, magic methods such as __str__ allow a programmer to define a print function for the class. Arithmetic magic methods such as __add__ allow a programmer to define what the + operator means as a class operation. Magic methods such as __ge__ and __eq__ allow a programmer to define relational operators for the class. These are but a few examples of how flexible Python can be when it comes to creating classes.

To review, see Going Deeper.

 

Unit 10 Vocabulary

This vocabulary list includes terms listed above that students need to know to successfully complete the final exam for the course.

  • structured programming
  • procedural programming
  • object-oriented programming
  • class
  • instantiated
  • object
  • magic methods
  • Init method 
  • self