Uploaded by Ngoc Nguyen

What is Encapsulation

advertisement
What is Encapsulation?
Encapsulation is one of the most principal concepts in Python, along with inheritance, abstraction,
and polymorphism. The purpose of performing encapsulation is to protect the sensitive data from
being accessed and accidentally manipulated.
To fully master encapsulation, there must be an understanding of the three levels of data protection.
“Public data” is the default format of all variables that can be accessed and modified globally,
which is not a good practice, especially for sensitive data. “Protected data”, which can be accessed
within the class and also by its subclasses, is data with a “single underscore” before the attribute.
To shield the variable from outside of the class access, use “double underscores” before the
variable name to turn it into “protected data”. However, when someone attempts to access
“protected data” from outside the class, they can still do it by following “name mangling” method.
Encapsulation offer some real benefits. The biggest one is to hide sensitive data to protect it from
outside access, which provides greater security. It also promises authority over the data input by
application users, which leads to lower maintenance and minimize errors. Finally, it’d be easier to
reuse code as well as brings users a good experience with the application.
Therefore, good developers should have a firm grasp of the concept in order to achieve clean and
easy-to-read code.
References
Encapsulation in python. AskPython. (2020, February 2). Retrieved October 31, 2021, from
https://www.askpython.com/python/oops/encapsulation-in-python.
Understanding python encapsulation clearly by practical examples. Python Tutorial - Master
Python Programming For Beginners from Scratch. (2021, October 17). Retrieved October
31, 2021, from https://www.pythontutorial.net/python-oop/python-private-attributes/.
What is encapsulation in python? Educative. (n.d.). Retrieved October 31, 2021, from
https://www.educative.io/edpresso/what-is-encapsulation-in-python.
Download