Object Oriented Programming Quick Review

Object = Something with unique characteristics and functions

what is the point of object oriented programming?

Sometimes, when coding, we want to create many of the same type of object. Rather than defining the same type of thing over and over again, we can create what is called a class, which is sort of like a template for a particular object, and every time we want to create a new one of these objects, we can create what is called an instance of a class. This prevents us from having to write a lot of redundant code. Every class contains an init method, which is what is called whenever we create a new instance of a particular class.

class Car:
    num_wheels = 4
    def __init__(self, color, make):
        self.color = color
        self.make = make
    def drive(self):
        return 'vroom vroom'
    def park(self):
        if num_wheels == 4:
            return 'In between the white lines!'
        else:
            return 'Oof, you better find a new spot :('
    def paint(self):
        return 'Added new ' + self.color + ' paint'
jeep = Car('black', 'jeep')

some important definitions

Instance attribute = A variable that is specific to a particular instance of a class

  • In the car class, color and make are examples of instance attributes because they are not the same for all cars

Class attribute = A variable that is a part of the class, and is thus shared across all instances of a class

  • num_wheels is a class attribute because it is defined outside of the init method, and thus shared across all instances

Whenever we try to find the value of a particular attribute (such as jeep.color), we always look in the instance first (at instance attributes), then at the class (class attributes), then to any parent classes (if there is any inheritance).

dot notation

When we access methods and variables in object oriented programming, we use what’s called dot notation. Since we are now working with methods, not regular functions, we can no longer just say things like drive() and park(). To access any method inside of a class, you can always do class_name.method(parameters).

Now you may be wondering, what is this self thing? Well, as a python convention, in a class we generally use self to refer to an instance of that particular class. That way, when we write self.some_attribute, we can get access to instance or class attributes associated with a particular instance. However, it is important to note that self is not a special keyword in python, and technically you could pass anything in as self, but depending on the body of the method, you may run into some errors if self doesn’t have all of the attributes that are requested in the method.

Since self represents the instance of a class, instead of doing class_name.method(parameters), we can also do self.method(any params other than self), since an instance of a class has access to all of the methods of its class.

For example, if we wanted to access jeep’s color, we would say:

jeep.color

And if we wanted to call the drive method, we could say:

jeep.drive()