Encapsulation
An objects variables should not always be directly accessible.
To prevent accidental change, an objects variables can sometimes only be changed with an objects methods. Those type of variables are private variables.
Important Note:
In C++ and Java, things are pretty straight-forward. There are 3 magical and easy to remember access modifiers, that will do the job (public, protected and private). But there’s no such a thing in Python. That might be a little confusing at first, but it’s possible too. We’ll have look at how do it right in Python.
Python doesn’t have any mechanisms, that would effectively restrict you from accessing a variable or calling a member method. All of this is a matter of culture and convention. In PYTHON, we can easily bypass protected/private variables/methods.
Protected Variables
Private Variables
Protected Methods
Private Methods
Protected Variables:
Protected members (in C++ and JAVA) are those members of the class which cannot be accessed outside the class but can be accessed from within the class and it’s subclasses. To accomplish this in Python, just follow the convention by prefixing the name of the member by a single underscore “_”.
AttributeError Traceback (most recent call last)
in 25 # Outside class will result in 26 # AttributeError ---> 27 print(obj1.a) 28 29 obj2 = Derived()
AttributeError: 'Base' object has no attribute 'a'
Bypassing Protected Variable
Example 2:
2
Calling protected member of base class:
2
Python doesn’t have any mechanisms, that would effectively restrict you from accessing a variable or calling a member method. All of this is a matter of culture and convention
Private Variable
Example 1
AttributeError Traceback (most recent call last)
in 14 obj1 = Base() 15 ---> 16 print(obj1.a) 17 18 obj2 = Derived()
AttributeError: 'Base' object has no attribute 'a'
ByPassing Private Variable
Example 2:
2
Calling private member of base class will give error
2
Protected Method:
Example 1:
updating software
driving
AttributeError Traceback (most recent call last)
in 12 redcar = Car() 13 redcar.drive() ---> 14 redcar.updateSoftware()
AttributeError: 'Car' object has no attribute 'updateSoftware'
ByPassing Protected Method:
updating software
driving
updating software
Private Method:
updating software
driving
AttributeError Traceback (most recent call last)
in 12 redcar = Car() 13 redcar.drive() ---> 14 redcar.__updateSoftware()
AttributeError: 'Car' object has no attribute '__updateSoftware'
Bypassing Private Method:
updating software
driving
updating software
Encapsulation example
Python does not have the private keyword, unlike some other object oriented languages, but encapsulation can be done.
Instead, it relies on the convention: a class variable that should not directly be accessed should be prefixed with an underscore.
Example 1:
123
123
Traceback (most recent call last):
File "test.py", line 10, in <module>
print(obj.__c)
AttributeError: 'Robot' object has no attribute 'c'
So what’s with the underscores and error?
A single underscore: Private variable, it should not be accessed directly. But nothing stops you from doing that (except convention).
A double underscore: Private variable, harder to access but still possible.
Both are still accessible: Python has private variables by convention.
Getters and setters
Private variables are intended to be changed using getter and setter methods. These provide indirect access to them:
Example 2:
This then outputs the variables values:
22
23
23
Example 3 :
1
2
2
0
AttributeError Traceback (most recent call last)
<ipython-input-32-0677a9635e82> in <module>
in 13 print (counter._JustCountersecretCount)
14 print (JustCounter._JustCountersecretCount)
---> 15 print (counter.__secretCount)
AttributeError: 'JustCounter' object has no attribute '__secretCount'
Example 4:
2
7
Traceback (most recent call last):
File "filename.py", line 13, in
print (myObject.hiddenVariable)
AttributeError: MyClass instance has no attribute 'hiddenVariable'
In the above program, we tried to access hidden variable outside the class using object and it threw an exception.
We can access the value of hidden attribute by a tricky syntax:
10
Example 6:
2
7
7
0
Last updated