Object-oriented programming is the bridge between beginner Python and intermediate Python. I highly advise that you understand beginner Python concepts, before following along with this tutorial, otherwise, you may be confused. This Object-Oriented Programming tutorial will be a part of a series, and every time I create a new article of the series, I will edit and add the link to the bottom of each article.
What Is Object-Oriented Programming (OOP)?
Python is an object-oriented language, meaning that everything is an object, whether it is a module, number, string, and the list goes on. In Python, we can make our own objects, by creating classes, which we will learn how to do in this tutorial.
Here we have an example of a class that creates a Dog object. It might look intimidating being new or unfamiliar to OOP, so let’s break things down.
An object is always created with the keyword class. A good convention for naming a class is using an uppercase letter for the name (in this case I picked Dog).
A class is essentially used when a category of objects have similar properties and attributes, but the objects differentiate from others by having different types of these properties and attributes.
In essence, a class is a blueprint for creating objects that have the exact behaviour, but different types of attributes and properties.
Classes will have different instances, so when we say create an object of a class, we are referring to one instance of the class. Objects can perform different methods (the functions in a class), that are defined within the class.
The occurrence of an object within a class.
Self is a parameter that references a current instance of the class and is used to access variables that belong within a class.
Note: self does not have to be named self and you could call it whatever you desire, although it is conventionally called self and it is best to keep it that way. Self is always the first parameter of any method in a class
__init__ is commonly referred to as a dunder (double underscore) or magic method. Classes will have their first method be an __init__ method (in some cases classes do not have an __init__ method, but that is not relevant right now). Essentially, this method will allow us to initialize the attributes of a class, and __init__ is always the first method that is called within a class.
Alright, so now that we understand a few key concepts, let’s take a walkthrough as to what is happening in this image.
Here, we are creating an object of a Dog. The attributes a dog has in this class are:
- The dog has a name (a string).
- The dog has an age (a float, if the dog is less than a year, its age can be represented as a decimal).
- The dog has a breed (a string).
- The dog has a weight (an integer).
Under the declaration of __init__ you see:
Essentially what we are doing here is initializing the attributes of an object. This step is important since different dogs can have different names, ages, breeds and weights, which is why we must do self.attribute = attribute.
Whenever we are referencing a specific attribute of an object, we must always put self.attribute.
Now moving onto the next part of the Dog class, we see a method called bark. When we call bark on an object of Dog, it will return “the name of the dog (self.name) goes bark!” We are using self.name because different objects of Dog will have different names, so by using self.name, we are referring to the specific name of that instance.
Notice, in def bark(self), we see that self is the only parameter within the function, and every function in a class must pass in self, because we are referring to a specific instance of a class.
It is worth noting that if you do not return a value in a function, it will return the value none, which is why I did return string, rather than print(string).
The next method within this class is called ispuppy, and this method will tell the user if the dog is a puppy or not a puppy. A puppy is defined if the Dog object’s age is less than or equal to one, otherwise, the Dog is not considered a puppy.
In the if statement, we are putting self.age, to reference the specific age of the instance of the object.
The last method within this class is called size. Based on the weight of an object’s instance, the function will return if the dog is small, medium, or big.
Lastly, we need to create an actual instance of the object Dog. I have called this object charlie, and here are its attributes.
- self.name = “Charlie”
- self.age = 1
- self.breed = Poodle
- self.weight = 30
Note, when you create an object of a class, all the parameters of __init__ must be provided, unless you set an attribute to None or False. Additionally, parameters must be in the order provided in __init__.
Attributes can also be predefined for example:
This is the same thing as creating charlie = Dog(‘Charlie’, 1, ‘Poodle, 30), but instead, you are defining the attributes within the __init__ method in the class. If you were to create the object after the class is created, but you also defined your attributes within the __init__ , different attributes can be overwritten declaring your object after the class is created.
Okay, so now that we have created charlie, which is an instance of Dog, we can call different methods on charlie.
First, let’s see if we can get the output to return the different attributes charlie has in __init__.
If we do:
our output will actually be:
Essentially, printing the object charlie, will result in an output that returns the memory address of charlie (different objects are located in different memory addresses).
Although, if we want to access different attributes of an object, this can be accomplished by the print object.attrbute.
Let’s try accessing charlie’s name:
Our terminal will now successfully output:
This can be done with any attribute that belongs to an object.
We can also call other methods on our object charlie.
Let’s try calling the object bark on charlie.
Note, since bark is a function, it must be called with parenthesis, otherwise, the output of the function will be the memory address of where it is located at.
Here is the output:
Now that we have successfully called our first method, let’s try calling the second one, ispuppy.
Here is the output:
Since we initialized that the instance charlie has an age of one, our function will return that it is a puppy. Although, if the instance’s age were to be greater than one, the output would return that charlie is not a puppy.
Lastly, now that we have successfully called our first two functions, we can proceed to call the final method on charlie, which is called size.
Here is the output:
Since the object charlie has a weight of 30, the output will return that Charlie is a small dog.
I highly encourage you to play around with this Dog class, by creating more instances and changing the different values of the output.
To practice making simple classes, here are a few ideas you can try:
- Class for different shapes (can calculate the perimeter or area of a shape)
- Classes for other animals
- Classes for people (a normal person, an employee at a company, etc.)
- A calculator class that can return different options (adding, subtracting, etc.)
Contact me for any inquiries 🚀
I hope you enjoyed reading the first tutorial of my Object Oriented Programming series in Python. Follow me so you can be alerted when the next articles of this series are published.
If you would like to recommend topics that you would like to see me write about (programming, A.I., or other), if you would like more resources, or have any general inquires, you can contact me by:
Links To Other Articles in This Series 🚀
Will be updated :)