Questions
9 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
09 / 19

How does Python implement encapsulation given that it has no true 'private' access modifiers?

Difficulty: 5/10
Encapsulation, Name Mangling, Properties

Encapsulation is by convention and name mangling, not enforced access control

Python has no private keyword. It relies on conventions: a single underscore prefix signals that an attribute is internal and subject to change, while a double underscore prefix triggers name mangling, where the compiler rewrites __attr inside a class body to _ClassName__attr. That mangling is not security; it prevents accidental override in subclasses but is still reachable if you know the mangled name. The real encapsulation tool in Python is a stable public API surface plus properties and slots to control access and mutation. The philosophy is that adults can be trusted; the language gives you tools to express intent rather than barriers to enforce it.

  1. 1

    Single underscore: internal by convention, no language enforcement, no mangling.

  2. 2

    Double underscore: name mangled to _ClassName__name, useful to avoid subclass collisions.

  3. 3

    Dunder names (two underscores on both sides) are reserved by the language and should not be invented.

  4. 4

    Use @property to expose a read-only or computed public attribute while keeping storage private.

  5. 5

    Use slots to restrict which attributes can be set, which gives a degree of structural encapsulation and saves memory.

  6. 6

    Trade-off: mangling can surprise users and complicate testing; many teams prefer a single underscore plus clear documentation.

  7. 7

    Common mistake: thinking __attr makes data inaccessible. It only changes the name.

  8. 8

    Version note: name mangling has been stable since Python 2 and applies to any identifier of the form __name inside a class body.

javascript

Scenario Questions

0-2 years experience

  1. 1You see _value and __value in a class. What is the difference?
  2. 2Can external code still read a double underscore attribute?

2-5 years experience

  1. 1A subclass defines the same __attr name as its parent and behavior changes unexpectedly. How does mangling explain this?
  2. 2You need a read-only attribute backed by internal state. Which tool do you use?

5-8 years experience

  1. 1You are designing a public library API and want to prevent users from depending on internals. How do you structure encapsulation?
  2. 2You need to allow attribute assignment only for a fixed set of names. How do you enforce this?

8+ years experience

  1. 1Design a class whose public API is stable across versions while internals evolve, using properties, slots, and versioned deprecation warnings.
  2. 2Compare Python's convention-based encapsulation with enforced access control in other languages, and discuss the testing and mocking implications.

Follow-up Questions

  • How does name mangling interact with subclasses and testing?
  • When is __slots__ a better encapsulation tool than underscore prefixes?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.