Understand Object Oriented in Python (Pythonic way)
This article will cover many things about object oriented in python, we will at first see what means “Object oriented programming”, what is a class, what is an attribute (class attribute, instance attribute..), what is a method(reserved method..), what is an instance, differences with class attributes and instance attributes, how to create a class with attributes, methods…
So to start, what means “Object oriented programming” ?
Object oriented programming (OOP) is a paradigm in programmation with the conception of everything is an object, these objects contains data (attributes) and code (methods). The idea behind the concept of object is that, that all the objects can interact with another object, this software design (paradigm) will allows you to think about your conception of code, your problematic, your projet, in terms of object and their interaction with each other.
Many programming language uses OOP as paradigm (C++, Python, Java, Php ..)
Now that we know a little more about this paradigm and the idea behind it, we can dig more into it.
What is a class ?
A class is basically where all the attributes and methods are, a class has a name, like Dog, this class named dog can have attributes like (Race, age..) and Methods (Eating, yelling..), this is really a basic example but it explained really quickly how we can imagine an object. A class is where you will initialize your object and create many action that your object can perform and interact with other object.
How to define a class in python :
Now, what is really an attribute in OOP ?
An attribute in OOP, is variable of data that represents the data of the class object, so for a dog class it will be race, age… This attribute can be create in three ways, public, private, protected.
The first one, is basically means that everybody(object) can access to the data easily of your object, so your class. It can be access within the class, outside the class. It’s not really recommended if you don’t want that other object can change his value or can be seen.
Public attribute (Class attribute):
Second one is protected attribute, the difference with public is that you cannot access directly with, only the object that been inherited (concept of inheritance in OOP that i will not cover today, parent and children relation, children inherit of attribute, methods of their parents, in brief) can access at this attribute, you can access to this attribute also within the class but not outside.
Protected attribute (class attribute):
The last one is, private attribute, this one can be accessed only within the class, not outside and not by children inherited and other object directly, you can create getters and setters to access at these attributes outside the class.
(You will see after 2 different ways to create attribute, instance attribute and class attribute)
Now that we saw attribute we need to understand methods :
A method is basically a piece of code, a function that does some action to the object or with an another object. As example with the dog, “yelling” could be a method to the class dog. A method has also as attribute different “security” to be accessed, private, public. The same way as attribute, with _ before the name of the method.
Create a method (Non pythonic way):
The keyword self in parameters represents the instance of the class, we will see more after about instance of class
Method with parameters :
It exist different reserved method , like __init__, __str__ and more
The most used one is __init__ , it called a constructor, it will initialize what you want when you will instance an object.
What’s means instanced an object ?
To use your class and object to interact with, you will need to instance them, this object will take has attributes and methods what you created in the class, it’s like giving life to someone :’).
As said before you can use a reserved method to initialize your object and give some data at the initialization, like this :
I used the keyword self to access at this attribute and give him a value but why i didn’t created the attribute before __init__ ?
This way to do it called, instance attribute, this attribute will affect and belong to only one object that been instanced, the way that we saw before is called class attribute. To create a instance attribute, it should be only in the __init__ method.
Create an instance attribute :
What is the advantage/differences of using instance or class attribute ?
Class attribute will shared the same attribute with all instances, with instance attribute only the object that been instanced has this attribute at the moment. If you want to change the value of a class attribute, the value will change to all object that been instanced with this class but with instance attribute only the “current” object will be changed.
Example : i will create one instance attribute and one class attribute and try to change them outside of the class with two instanced object to see if with the class attribute, all the attribute of all instanced object has changed or not
Without changing any attributes we have this value :
Now, i want to change the value of the class attribute “health” to see if all the instanced object changed too:
We can assume from the result that if we change a class attribute in this way, all the object instanced related to the class will be affected.
What happen if i change the age, so the instance attribute of labrador1 ?
Only one object has change, the instance attribute of labrador1.
Back to our example of instance of a dog, to initialize a value to the instance attribute of our object, just need to do like this :
So now, our beautiful dog has an age of 9 years :)
You want that our labrador, yell ? Easy :)
That’s it, this how you can call methods to do some action with your object :).
But before finish this article, i need to show you the best way to create your class and methods, it called the pythonic way. If you remember i talked before about getters and setters, these two is really important and clean way to do all your classes.
Create methods (Pythonic way)
The getter is a method that return the value of an attribute, this attribute can be private at first but with a getter its more secure to print the value of this attribute (you can return whatever :) )
You can now access to the attribute like this :
The setter is a method that will modify attribute of your class but in the safe way, you can add multiple condition and check with the value passed to the object to change his value.
In the __init__ automaticly, the setter will be called with the help of @age.setter that will search for a setter of the attribute age.
Bonus: __dict__ is a reserved method, a dictionary (or other mapping object) that store all attributes of an object, each instance of an object is stored in the dictionary. If you want to see what stored __dict__ you can display it like this :
This is all for today, i hope you enjoy reading it and you learn something :)