Encapsulation 封装 of class in Python
In software development, encapsulation is one of the four foundational pillars of OOP.

Encapsulation 封装 of class is a concept of bundling of data and the methods that operate on that data into a single unit.


Private Attributes (Data hiding) / Behaviour

For example, a mobile phone, may allow all of the user to have a feature like taking photograph, calling to others, connect to the internet, ...
However a phone would have it hidding data (Private attributes) like control the voltage for the processor, memory management, drivers information, which is not open to the user.
- So, there is a
2 private class memberswhich isprivate attributesandprivate behaviour.
Usage
py
# Private attributes
__attrs = ""
# Private behaviour/ method
def __fun(x):
...- Add
double undersocrefor the variable and methods. (__var = x) / (def __f(x):)
Example
class-private-attribute-behaviour.py
py
class Phone:
phone_name = None
IMEI = None
__maximum_voltage = 1.25
def __init__(self, phone_name, IMEI):
self.phone_name = phone_name
self.IMEI = IMEI
def connect_to_network(self, status: bool):
if status:
print("Connected to network")
else:
print("Disconnect from network connection")
def __run_on_single_core(self):
print(f"Phone is run on single core only. Maximum voltage for cpu is {self.__maximum_voltage}v")
print(__maximum_voltage)
p1 = Phone("Star", 1879986652080)
print(p1.phone_name, p1.IMEI)
print(p1.__maximum_voltage)Output:
1.25
Star 1879986652080
Traceback (most recent call last):
File "d:\jiale-note\demo-examples\Python\Language\class-private-attribute-behaviour.py", line 21, in <module>
print(p1.__maximum_voltage)
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Phone' object has no attribute '__maximum_voltage'. Did you mean: '_Phone__maximum_voltage'?ERROR
- The
private attributes / behaviourcan't be access on outside of the class. - However these
private attributes / behaviourcan be access on outside of the class, only when they are use by otherattributes / behaviourthat are not private.
class-private-attribute-behaviour.py
py
class Phone:
phone_name = None
IMEI = None
# Private attr
__maximum_voltage = "1.25"
def __init__(self, phone_name, IMEI):
self.phone_name = phone_name
self.IMEI = IMEI
def connect_to_network(self, status: bool):
if status:
print("Connected to network")
else:
print("Disconnect from network connection")
# Private behaviour
def __run_on_single_core(self):
print(f"Phone is run on single core only. Maximum voltage for cpu is {self.__maximum_voltage}v")
def battery_level(self, percentage_of_battery: float):
if percentage_of_battery <= 20:
# Using Private behaviour for a non-private function
self.__run_on_single_core()
else:
print("Multi-core is enabling.")
# use inside of a class
print(__maximum_voltage)
p1 = Phone("Star", 1879986652080)
print(p1.phone_name, p1.IMEI)
p1.connect_to_network(True)
p1.battery_level(49)
p1.battery_level(19.334)Output:
1.25
Star 1879986652080
Connected to network
Multi-core is enabling.
Phone is run on single core only. Maximum voltage for cpu is 1.25v