0
0
Pythonprogramming~10 mins

Name mangling in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Name mangling
Define class with __private attribute
Create instance of class
Access __private attribute inside class method
Access __private attribute outside class
Python changes __private to _ClassName__private
Access succeeds using mangled name
Name mangling changes __private names inside classes to _ClassName__private to avoid accidental access from outside.
Execution Sample
Python
class MyClass:
    def __init__(self):
        self.__secret = 42

obj = MyClass()
print(obj._MyClass__secret)
This code shows how Python changes __secret to _MyClass__secret and how to access it.
Execution Table
StepActionVariable/AttributeValueNote
1Define class MyClassMyClassclass objectClass created with __secret attribute
2Create instance objobjMyClass instanceobj.__secret is not directly accessible
3Inside __init__, set self.__secretobj.__secret42Name mangled to obj._MyClass__secret internally
4Try print(obj.__secret)obj.__secretAttributeErrorDirect access fails, attribute is mangled
5Print obj._MyClass__secretobj._MyClass__secret42Access succeeds using mangled name
💡 Execution stops after printing the mangled attribute value 42
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
obj.__secretundefinedundefined42 (mangled as _MyClass__secret)42
obj._MyClass__secretundefinedundefined4242
Key Moments - 2 Insights
Why can't we access obj.__secret directly?
Because Python changes __secret to _MyClass__secret internally (see step 4 in execution_table), so obj.__secret does not exist.
How does Python prevent accidental access to private attributes?
By name mangling: it renames __secret to _ClassName__secret, making it harder to access from outside (see step 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 4 when trying to print obj.__secret?
AIt raises AttributeError
BIt prints 42
CIt prints None
DIt prints _MyClass__secret
💡 Hint
Check step 4 in execution_table where direct access to obj.__secret fails
At which step is the private attribute actually set inside the object?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at step 3 where self.__secret is assigned the value 42
If the class name changes to YourClass, what would be the mangled name for __secret?
A_MyClass__secret
B_YourClass__secret
C__secret
D_secret
💡 Hint
Name mangling uses _ClassName__attribute format, see concept_flow and execution_table steps
Concept Snapshot
Name mangling in Python:
- Attributes starting with __ are renamed internally.
- Format: __attr becomes _ClassName__attr.
- Prevents accidental external access.
- Access with obj._ClassName__attr if needed.
- Not true private, just name changed.
Full Transcript
Name mangling is a Python feature that changes attribute names starting with double underscores inside classes. When you write __secret inside a class named MyClass, Python changes it to _MyClass__secret internally. This helps avoid accidental access from outside the class. For example, setting self.__secret = 42 inside __init__ actually creates obj._MyClass__secret. Trying to access obj.__secret directly causes an error because that name does not exist. To access the value, you use obj._MyClass__secret. This is not true privacy but a way to reduce naming conflicts. If the class name changes, the mangled name changes accordingly.