Object Oriented Programming
In programming, an object is a data structure that has user-defined attributes and methods associated with it. Objects are very useful in programming because they allow us to store lots of information and run specific functions according to object-oriented programming-- for instance, we can create a Dog object with attributes like name and age and run functions like do_a_trick.
Methods and Attributes (Properties)
Methods are like functions for classes, while attributes are variables for classes. Lets take a look at how these appear in a class:
In the Dog object above the attributes are self.color
, self.age
, and self.cuteness
.
Note the difference between attributes and variables--attributes are a type of variable that can only be referenced with respect to an object.
So how do we use an object? It's actually fairly simple:
When we create Spots above, Spots is a variable that belongs to the Dog class!
This means Spots is an instance of that class--we can actually see this in the last line of code, where type(Spots)
returns Dog!
You'll see that you can access the attributes of the Spots instance by typing its name, a period, and what attribute you want to access (for example, Spots.age
).
Similarly, if we want to use a method (fuction) of an object, we type the instance's name (Spots), a period, the function name, brackets, and whatever arguments
that method needs.
Here, when we call Spots.birthday()
, we run the birthday function, which increases Spots' age by 1.
Since this directly updates the value of the age attribute, the next time we use Spots.age, it'll be 4!
Note that the argument self
is only used inside the code for the Dog object, and not for any instances.
What this means is when you create Spots, you don't need to enter anything in the brackets for self
--as seen on line 10 in the code above, we only need to set Spots' age and color.
Note also that we don't necessarily need all attributes to come from arguments! self.cuteness
is set to 100 and doesn't rely on any arguments.
__eq__ Special Method
__eq__ is a special method that you can add to a class, which would allow you to compare two objects using "==". For example, we can change the above code to include a special method for equality.
Note that the above __eq__(self, other) takes in two arguments, self and other. You can think of self and other to be the two things you are comparing with "==". You want to define how self == other. First we want to make sure other is also an instance of Dog, and therefore we do isinstance(other, Dog). Then we define the equality of the two objects by the equality of their attributes. To access an attribute of the self object, we do self.color, and similarly for other.
Inheritance
Now that we know how to create a Dog, our next step might be to create specific types of Dogs. For example, we can create golden retrievers, border collies, and, God rest their demonic souls, chihuahuas. Obviously, each of these breeds are all dogs--so instead of creating an entirely new object for each of them, we can actually make them subclasses of the Dog object. In the following example, we'll be creating a racing_dog that inherits the Dog object's attributes and adds a property for speed; it will also be able to race in addition to having birthdays. Lastly, when a racing_dog has a birthday, the Racing Committee will give it a special birthday congratulation, so we want to add to the existing birthday function.
class Dog(object):
def __init__(self, color, age):
self.color = color
self.age = age
self.cuteness = 100
def birthday(self):
self.age += 1
class racing_dog(Dog):
def __init__(self, color, age, speed):
super().__init__(color, age) # we go to Dog's __init__ and do everything there with the given color and age
self.speed = speed # this wasn't defined in Dog, so we have to write it out here
def race(self):
self.speed *= 2 # multiply its speed by 2
def birthday(self):
super().birthday() # we go to Dog's birthday function and do everything there
print("Happy %dth birthday!" %self.age) #then we print out this birthday message
Buddy = racing_dog("yellow", 5, 3)
print("Buddy's initial speed is %d" %self.speed)
Buddy.race()
print("When racing, his speed is %d" %self.speed)
Buddy.birthday() # this will print out the birthday message
Note: Unfortunately our online coderunner isn't advanced enought to handle inheritance, so you're going to have to copy and paste this code into VS Code and run it there. D:
We see that super()
essentially references the superclass, Dog, and runs the corresponding function there!
Lastly, if we want to check the type/instance of a subclass, type()
and isinstance()
start to differ. Example: