How does Python do encapsulation, since it has no private keyword?

Python classes and OOP · Freshers and experienced

Model answer

By convention: a name with one leading underscore (_value) means "internal, do not use from outside". Two leading underscores trigger name mangling (__value becomes _Class__value) to avoid clashes in subclasses. Use properties to control access. Nothing is truly private, it relies on convention and trust.

Learn this topic

Classes and Objects in Python: A Gentle Introduction in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions