Part III: Classes, Objects, Attributes & Methods
It is the third part of Beautiful Python series by Tk. If you are a totally beginner developer learning Python, you should start with part I & part II of this series. The first part we learn about the history about Python & very basic stuff like variables, control flow (if-else statements) and looping. For the second part we talk more about Python Data Structures, how it works & how we can iterate through it. In this part we have a conversation about Object Oriented Programming & Python.
Classes & Objects
A little bit of theory:
Objects are a representation of the real world objects like cars, dogs, bike, etc. The objects share two main characteristics: data and behavior.
Cars have data like number of wheels, number of doors, seating capacity and also have behavior: accelerate, stop, show how much fuel is missing and so many other.
We call data as attributes and behavior as methods in object oriented programming. Again:
Data ? Attributes & Behavior ? Methods
And a Class is the blueprint from which individual objects are created. In the real world we often find many objects with all the same type. Like cars. All the same make and model (have an engine, wheels, doors, ?). Each car was built from the same set of blueprints and has the same components.
Python Object Oriented Programming mode: ON
Python as an Object Oriented programming language has these concepts: class & object.
A class is a blueprint, a model for its objects.
So again, a class it is just a model, a way to define attributes and behavior (as we talk in the theory section). As an example, a Vehicle class has its own attributes that defines what is a Vehicle object. Number of wheels, type of tank, seating capacity and maximum velocity are all attributes of a vehicle.
With this in mind, let?s understand Python syntax for classes:
We define classes with class statement and that?s it. Easy!
And objects are instances of a class. We create an instance by calling the class.
Here car is an object (or instance) of the class Vehicle.
?A car is a instance of the class of objects known as vehicles.?
Remember that our Vehicle class has 4 attributes: Number of wheels, type of tank, seating capacity and maximum velocity. We set all this attributes when creating a vehicle object. So here we define our class to receive data when instantiates it.
We use the init method. We call it a constructor method. So when create the vehicle object, we can define this attributes. Imagine that we love the Tesla Model S and we want to create this kind of object. It has 4 wheels, it is an electric car so the tank?s type is electric energy, it has space for 5 seats and the maximum velocity is 250km/hour (155 mph). Let?s create this object! 🙂
4 wheels + Electric type of tank + 5 seats + 250km/hour maximum speed.
All attributes set. But how can we access this attributes values? We send a message to the object asking about them. We call it a method. It?s the object?s behavior. Let?s implement it!
This is an implementation of two methods: number_of_wheels and set_number_of_wheels. We call it getter & setter. Because the first get the attribute value, and the second set a new value for the attribute.
In Python, we can do that using @property (decorators) to define getter and setters. Let?s see it with code!
And we can use this methods as attributes:
This is slightly different than defining methods. The methods work as attributes. For example, when we set the new number of wheels, we don?t pass 2 as parameter, but set the value 2 to number_of_wheels. This is one way to write pythonic getter and setter code.
But we can also use methods to other things like ?make_noise? method. Let?s see it!
When we call this method, it just returns a string ?VRRRRUUUUM?.
We learnt a lot of things about Python object oriented programming:
- Objects & Classes
- Attributes as objects? data
- Methods as objects? behavior
- Using Python getters and setters & property decorator
I hope you guys can appreciate the content and learn more about how Python object oriented programming works. This is just the third part of Beautiful Python series. For part 2 of Python OOP we are going deep in other concepts!
If you didn?t see the part I & part II, get the role series.
If you want a complete Python course, learn more real-world coding skills and build projects, try One Month Python Bootcamp. See you there! ?
Have fun, keep learning & always coding!
I hope you liked this content. Support my work on Ko-Fi
My Twitter & Github. ?