Python 101: Object Oriented Programming part 2
Part IV: Encapsulation, Inheritance
It is the fourth 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.
I also advice you to read the first part of Object Oriented Programming with Python
Let’s start! :)
Encapsulation: Hiding Information
Encapsulation is a mechanism that restricts direct access to objects’ data and methods. But at the same time, it facilitates operation on that data (objects’ methods).
“Encapsulation can be used to hide data members and members function. Under this definition, encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition.” — Wikipedia
All internal representation of an object is hidden from the outside. Only the object can interact with its internal data.
First, we need to understand how public
and non-public
instance variables and methods work.
Public Instance Variables
For a Python class, we can initialize a public instance variable
within our constructor method. Let’s see this:
Within the constructor method:
Here we apply the first_name
value as an argument to the public instance variable
.
Within the class:
Here, we do not need to apply the first_name
as an argument, and all instance objects will have a class attribute
initialized with TK
.
Cool. We have now learned that we can use public instance variables
and class attributes
. Another interesting thing about the public
part is that we can manage the variable value. What do I mean by that? Our object
can manage its variable value: Get
and Set
variable values.
Keeping the Person
class in mind, we want to set another value to its first_name
variable:
There we go. We just set another value (kaio
) to the first_name
instance variable and it updated the value. Simple as that. Since it’s a public
variable, we can do that.
Non-public Instance Variable
We don’t use the term “private” here, since no attribute is really private in Python (without a generally unnecessary amount of work). — PEP 8
As the public instance variable
, we can define the non-public instance variable
both within the constructor method or within the class. The syntax difference is: for non-public instance variables
, use an underscore (_
) before the variable
name.
“‘Private’ instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g.
_spam
) should be treated as a non-public part of the API (whether it is a function, a method or a data member)” — Python Software Foundation
Here’s an example:
Did you see the email
variable? This is how we define a non-public variable
:
We can access and update it.
Non-public variables
are just a convention and should be treated as a non-public part of the API.
So we use a method that allows us to do it inside our class definition. Let’s implement two methods (email
and update_email
) to understand it:
Now we can update and access non-public variables
using those methods. Let’s see:
- We initiated a new object with
first_name
TK andemail
tk@mail.com - Printed the email by accessing the
non-public variable
with a method - Tried to set a new
email
out of our class - We need to treat
non-public variable
asnon-public
part of the API - Updated the
non-public variable
with our instance method - Success! We can update it inside our class with the helper method
Public Method
With public methods
, we can also use them out of our class:
Let’s test it:
Great — we can use it without any problem.
Non-public Method
But with non-public methods
we aren’t able to do it. Let’s implement the same Person
class, but now with a show_age
non-public method
using an underscore (_
).
And now, we’ll try to call this non-public method
with our object:
We can access and update it.
Non-public methods
are just a convention and should be treated as a non-public part of the API.
Here’s an example for how we can use it:
Here we have a _get_age
non-public method
and a show_age
public method
. The show_age
can be used by our object (out of our class) and the _get_age
only used inside our class definition (inside show_age
method). But again: as a matter of convention.
Encapsulation Summary
With encapsulation we can ensure that the internal representation of the object is hidden from the outside.
Inheritance: behaviors and characteristics
Certain objects have some things in common: their behavior and characteristics.
For example, I inherited some characteristics and behaviors from my father. I inherited his eyes and hair as characteristics, and his impatience and introversion as behaviors.
In object-oriented programming, classes can inherit common characteristics (data) and behavior (methods) from another class.
Let’s see another example and implement it in Python.
Imagine a car. Number of wheels, seating capacity and maximum velocity are all attributes of a car. We can say that an ElectricCar class inherits these same attributes from the regular Car class.
Our Car class implemented:
Once initiated, we can use all instance variables
created. Nice.
In Python, we apply a parent class
to the child class
as a parameter. An ElectricCar class can inherit from our Car class.
Simple as that. We don’t need to implement any other method, because this class already has it (inherited from Car class). Let’s prove it:
Beautiful. ❤
That’s it!
We learnt a lot of things about Python object oriented programming:
- Encapsulation: hiding information
- Inheritance: behaviors and characteristics
I hope you guys can appreciate the content and learn more about how Python object oriented programming works. This is the forth part of Beautiful Python series.
If you didn’t see the part I, part II & part III, 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!