Explore core concepts of Python encapsulation, including access conventions, name mangling, property usage, and preventing mutable reference leaks. Perfect for leveling up your understanding of state control and best practices.
Which statement best describes encapsulation in Python compared to languages like Java?
Explanation: Python does not enforce access restriction through technical means, relying instead on social conventions and responsible coding. There are no compiler-enforced private members or forbidden attribute access. Python also does not have a 'private' keyword.
Where are instance attributes stored in a typical Python object?
Explanation: Python instance attributes are stored in the object's __dict__ dictionary. The other options are not standard Python mechanisms for storing attributes.
What is the main purpose of prefixing an attribute with a single underscore in Python (e.g., _x)?
Explanation: A single underscore signals that an attribute is for internal use, as recommended by PEP 8. Python does not enforce privacy, nor does it encrypt or restrict subclass access based on underscores.
What is the effect of using a double underscore prefix (e.g., __x) in a Python class attribute?
Explanation: Double underscores trigger name mangling, renaming attributes to include the class name and making accidental overrides in subclasses less likely. The attribute is not truly private, encrypted, or made immutable.
Why can sharing a mutable object like a list between an object's attribute and external code break encapsulation in Python?
Explanation: Sharing a mutable reference allows outside code to mutate the internal state without the object's knowledge. Python does not copy lists automatically, nor does it make them read-only, and mutable attributes are allowed.
How does using a defensive copy (e.g., deepcopy) in a constructor help maintain encapsulation?
Explanation: Defensive copying ensures the object doesn't share mutable references, preserving control over attribute mutations. It does not encrypt, validate every input, or ban mutable attributes.
What is one advantage of using the @property decorator for an attribute in a Python class?
Explanation: @property allows getter/setter logic for validation or computed access in the attribute style. It does not make attributes private, does not turn them into class variables, and does not disable deletion directly.
How does a setter method with the @property decorator contribute to robust encapsulation?
Explanation: A property setter enables the class to check or validate data before updating internal variables, ensuring data integrity. It does not prohibit reading, does not enforce immutability globally, and does not affect inheritance.
What is a key benefit of using @dataclass(frozen=True) in Python?
Explanation: @dataclass(frozen=True) ensures that once created, an object's fields cannot be modified, supporting immutability. It does not perform deepcopy, block access, or enable runtime type checking by default.