Python Tutorial 0/52 lessons ~6 min read Lesson 28

    Properties & Encapsulation

    Python doesn't enforce private attributes — convention uses underscore.

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Python doesn't enforce private attributes — convention uses _underscore. Use @property for managed access without breaking callers.

    Understanding the topic

    Core concepts to understand:

    • _x — convention for internal.
    • __x — name-mangled (rarely used).
    • @property turns method into attribute.
    • @x.setter for write access.

    Syntax reference

    Visual flow / code:

    python
    class Temperature:
    def __init__(self, celsius: float):
    self._celsius = celsius
    @property
    def celsius(self) -> float:
    return self._celsius
    @celsius.setter
    def celsius(self, value: float):
    if value < -273.15:
    raise ValueError("Below absolute zero")
    self._celsius = value
    @property
    def fahrenheit(self) -> float:
    return self._celsius * 9/5 + 32
    t = Temperature(25)
    print(t.fahrenheit) # 77.0

    Execution workflow

    1Properties & Encapsulation Workflow
    1 / 4

    _x

    convention for internal.

    Real-world use

    Properties let you start with simple public attributes, then add validation / computation later without breaking your API. This is huge for library design.

    Best practices

    • Start with public attrs; add @property only when logic appears.
    • Underscore signals internal — don't enforce.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat is @property?+

    Answer

    @property is a built-in descriptor that lets a method be accessed like an attribute, so obj.area calls the getter. You can add a setter with @area.setter and a deleter with @area.deleter. It lets you start with a plain public attribute and later add validation or computation without changing callers.

    Follow-up

    How does a property differ from a plain descriptor class?
    2BeginnerQuestionPublic vs private in Python?+

    Answer

    Python has no access modifiers; it uses naming conventions. A plain name is public, a single leading underscore (_x) means internal by convention, and a double leading underscore (__x) triggers name mangling to _Class__x to avoid accidental clashes in subclasses. None of these are enforced by the interpreter.

    Follow-up

    What does from module import * do with underscore names?
    3IntermediateQuestionWhy no real private?+

    Answer

    Python's design philosophy is that we are all consenting adults: the language trusts developers and relies on convention instead of enforcement. Attributes live in an instance __dict__ and can be reached with getattr, so even mangled names are only renamed, not protected. Real protection has to come from process or API boundaries, not from the class.

    Follow-up

    How would you stop callers from changing a value after construction?

    Summary

    In summary: @property = invisible upgrade path. Convention > enforcement.

    Ready to mark this lesson complete?Track your journey across the entire course.