Object-oriented Programming in Python 🐍
Object-oriented Programming (OOP) is a computer programming model that helps one organizes their software design around data, class and object rather than functions and logic.
Class is the blueprint used to create user-defined data structure. It holds the common attribute and behaviors of the object. For example: the class “Ride” will hold object “car”, “plane” and “bike”.
Object is an instance of a class. It has both a property (variables) and behavior (methods). The python object “Car” will have properties; “make”, “model”, “four_wheels” and behaviors; “start”, “stop”.
Method is the behavioral factor of the object. Modifying a method in the class “Ride” would affect all instances --“car”, “plane” and “bike”-- of it.
Instance is simply any object made from the class. An instance of the “Ride” class would contains real unique data. A class is like a form…An instance is a filled variation of that form.
it is good practice to identify all the objects you want to manipulate and how they relate among themselves. This is data modeling.
Principles of OOP
State, variables, and methods should be private unless they are being declared public. An object “car” of a class “Ride” can manage itself via methods “start” and other class “House” cannot touch it expect allowed to. We can only use the methods to communicate with our object. If we want another class “House” to change the state of the “car” object, we have to create public methods “over_ride_and_start” to modifies or invoke the start variable. This connection is encapsulation.
Hide all implementation of the class from anything outside the class. Our object should hide details and only expose relevant information to another object. Thus public method should be created so object can be called. The “Al voice recognition” may start our “car” object (in the “Ride” class) and open the “Lights” object (in the “Home” class) without having access to the details of our car. This makes maintenance of an extremely large database less daunting and lowers the chances of bugs.
Child class should have a mechanism that enables it to inherit behavior and properties from the parent class. Reusability is the advantage here. Child class “car”, “boat” objects should acquire properties (“has_an_engine”, “can_be_used_for_transport”) from the parent class “Ride”. However, they should have their own unique properties.
Interfacing with objects and receive different forms or results. This allows us to implement a class exactly as intended for the parent class. This prevents errors in types and each sub-class keeps its own methods. Our class “Ride” has a speed method. Our child class; “Object”, “boat” and “plane” can utilize this method. However, they would each have their own iteration of the speed method.
OOP in Python 🐍
Let's Begin! 🚀
Class is defined with `class` followed by the name of the class.
At the moment, our car class is without attributes or methods. Let's instantiate our object class by calling the class.
Every instance of our class is stored in a new memory address which means every instance of the Car class represents two different objects. Wanna try?
Our class has a docstring attribute called `__doc__` which holds the documentation of the object.
Let’s make our class interesting by giving it attributes. There are two types; Class attributes and Instance attributes. Every attribute in our class would be defined in a method called `__init__()` with self as the first parameter. These are called instance attributes. However, class attributes are attributes that have the same value for all instances of that class. Hence it would be assigned outside the `__init__()` method. All instances of car class (model and colour) are four-wheeled.
Let’s give our instance attributes parameters values. 🧨
We have four Car instances with each having unique values. Let’s access the data in these attributes.
Toyota black four four
Like I mentioned all instances in a class have .wheels, .model and .colour. However, the values of the instances attributes (.model and colour) would differ; encapsulation and polymorphism. We can also update and modify the values in our instance without changing the values.
It worked!! 😎 So objects are mutable. Let’s look at instance methods. Just like `__init__()`, the first parameter is also always self and is defined in our class. Let’s create two instance methods; speed and details.
Let's apply these new methods.
this Toyota is yellow This Toyota is 280 at top speed
Let’s look at inheritance. Let’s say we want to add a new class “engine”. We can’t always specific the engine for each car. Let’s create a child class for it
Now let’s instantiate these engines for the cars.
Let’s try some basic functions.
<class '__main__.Gas_engine'> True <class '__main__.Hybrid_engine'> False
Thus, all objects created from the child class is an instance of the parent (car) but might not be in the same child class (Gas_engine and Hybrid_engine). Let’s give it the parent’s functionality specific to it (Inheritance)
Ford Speed; 210
A change in the parent class wouldn’t affect the child class.
This Toyota is 367 at top speed Ford Speed; 467
However, if we didn’t want this to happen. We would need to use the .super built-in function for the child class. 🙃
This Ford is 210 at top speed
In this article, we discussed Object-oriented Programming and the principles which govern it.
Then we looked at how to define a class, how to instantiate an object from a class and giving this class some sought of documentation using `__doc__`
Let's not forget we discussed instance and class attribute along with defining properties and behaviors of an object. We also discussed some function such as `type()`, `isinstance()` and `super()`. 🚀
I hope you found this notebook helpful. If you did, please share it on your favorite social media so other folks can find it, too. Thanks 👍