0
0
Pythonprogramming~10 mins

Private attributes in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Private attributes
Create object
Set private attribute with __
Access private attribute inside class
Try to access private attribute outside class
Error or no direct access
Use name mangling to access if needed
This flow shows how private attributes are set and accessed inside a class, and how direct access outside is blocked but can be done with name mangling.
Execution Sample
Python
class MyClass:
    def __init__(self):
        self.__secret = 42
    def reveal(self):
        return self.__secret
Defines a class with a private attribute __secret and a method to reveal it.
Execution Table
StepActionVariable/AttributeValueNote
1Create MyClass instanceobjMyClass objectObject created
2Set private attributeobj._MyClass__secret42Inside __init__, private attribute set (name mangled)
3Call reveal methodobj.reveal()42Accesses private attribute inside class
4Try direct accessobj.__secretErrorAttributeError: not accessible directly
5Access with name manglingobj._MyClass__secret42Works due to name mangling
💡 Execution stops after demonstrating private attribute access and restrictions.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
obj._MyClass__secretN/A4242 (inside reveal)42 (via name mangling)
Key Moments - 3 Insights
Why can't we access obj.__secret directly outside the class?
Because Python changes the name internally (name mangling) to _ClassName__secret, so direct access fails as shown in step 4 of the execution_table.
How does the reveal method access the private attribute?
Inside the class, the private attribute __secret is accessible normally, as shown in step 3 where reveal returns 42.
What is name mangling and how does it help?
Name mangling changes __secret to _MyClass__secret internally, allowing access if needed outside the class, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens when we try to access obj.__secret directly?
AIt returns None
BIt returns 42
CIt raises an error
DIt creates a new attribute
💡 Hint
See step 4 in the execution_table where direct access causes an error.
At which step does the private attribute get set inside the object?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Step 2 shows setting obj._MyClass__secret to 42.
If we change the class name to YourClass, how would we access the private attribute outside?
Aobj._YourClass__secret
Bobj.__secret
Cobj._MyClass__secret
Dobj.secret
💡 Hint
Name mangling uses _ClassName__attribute, so with YourClass it changes accordingly.
Concept Snapshot
Private attributes start with __ to hide them from outside access.
Inside the class, you can use them normally.
Outside, direct access causes error.
Python changes the name internally (name mangling).
Access outside is possible with _ClassName__attr syntax.
This helps protect data but is not full security.
Full Transcript
This lesson shows how private attributes work in Python classes. When you create an attribute with two underscores at the start, like __secret, Python changes its name inside the object to include the class name. This is called name mangling. It helps keep the attribute hidden from outside code. Inside the class, you can use the private attribute normally, like in the reveal method. But if you try to access it directly from outside, you get an error. You can still access it outside by using the mangled name, which is _ClassName__secret. This is a way to protect data inside objects but still allow controlled access.