Python ClassesLesson 46Free preview
Python Encapsulation
This lesson is part of Python · All levels
Summary
Encapsulation combines data and methods in a class while limiting direct access to internal details. Python has no private modifier like other languages, but rather through a name convention: one _x underscore for internal and two __x underscore that triggers name mangling. Getters, setters, or properties are then used to control access to the data.
Key points
- Encapsulation = packing data and methods into a class and restricting direct access to internal attributes.
- The name is a protected convention: accessible, but not recommended from outside.
- The name triggers the name mangling to be a class name so it's hard to access from the outside.
- Provide an official method of reading and changing data in the form of a getter, setter, or property.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.