This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Objects


One of the key aspects of Python is that the language is Object Oriented (OO). The  OO paradigm is a conceptual and organizational methodology that we use to create reusable code and develop applications efficiently. Objects are not inherently complex or difficult to work with, in fact, they are simple to create and re-use throught our programming endeavours. 

Objects have two main characteristics:

  • methods
  • attributes

Methods are another word for functions contained within an object. Attributes are variables within an object. Consider my toaster:

>>> my_toaster = Toaster('shiny', 'crispy')
>>> my_toaster.make_toast(bread)
# returns toast.
>>> my_toaster.color
silver
>>> my_toaster.darkness_setting
crispy

In the first line, I create an instance of the Toaster class (notice the capital T in Toaster) and assign it to 'my_toaster'. I tell the Toaster class that my_toaster is shiny and makes crispy toast. Afterwords, I can interact with my_toaster by accessing its methods (functions) and attributes (variables).

Notice that the function and attributes are accessed by appending a dot ('.') and the relevant name/function call. This is called, appropriately, 'dot notation' and is designed as a convenience for us programmers.

So, creating an instance of a class seems easy, right? Lets cover the basics of creating a class. We will go ahead and peer inside the Toaster class.

class Toaster:
"""Found in kitchens around the world. Excellent for toasting bread."""
    def __init__(self, color, darkness_setting):
        self.color = color
        self.darkness_setting = darkness_setting
       
    def make_toast(bread):
            # Code to make toast
            return toast

The first line above is the class definition. The word class preceeds the name (uppercase as a convention) and a colon. The following line is a documentation string, giving further details about the toaster class.

The __init__() method is called a class constructor. Python calls the __init__() method whenever a class instance is created. In this case, the __init__() method simply assigns values to color and darkness_setting attributes  for the instantiated class, referenced by self. Python instances use the self keyword  to refer to their internal state.

Next, we define the make_toast() function, which takes one parameter, named bread. The make_toast() function is nested within the Toaster class and is therefore a method of Toast, rather than simply being a function. make_toast() would normally have some additional code to process the input data, but we can simplify things for this example.

The last thing that the make_toast() method does is return the toast object (yes, this too is an object). When an object returns data, the data may be assigned to a variable for later use.

>>> our_toaster = Toaster('turquois', 'high heat')
>>> snack = our_toaster.make_toast(wheat_bread)
>>> myself.eat(snack)

Further Reading

Task

  1. Think of two objects that you would encounter in your daily experience, other than toasters :-)
  2. List several attributes for each object
  3. List at least one method that the object employs
  4. Create Python pseudocode for each object, don't forget the __init__() function.
  5. Post your pseudo code to this task discussion.

Extra Credit: Have one of the objects return information that is utilized by the other object. 

Task Discussion