Name mangling in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time it takes to run code changes when using name mangling in Python.
We want to see how the program's steps grow as the input or usage grows.
Analyze the time complexity of the following code snippet.
class MyClass:
def __init__(self, value):
self.__hidden = value
def get_hidden(self):
return self.__hidden
obj = MyClass(10)
print(obj.get_hidden())
This code uses name mangling to hide a variable inside a class and then accesses it through a method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the mangled variable inside the method.
- How many times: Once per method call; no loops or recursion here.
Since there are no loops or repeated steps, the time to access the mangled name stays about the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 access |
| 100 | About 1 access |
| 1000 | About 1 access |
Pattern observation: The time does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to access a mangled name is constant and does not depend on input size.
[X] Wrong: "Name mangling makes accessing variables slower because of extra steps."
[OK] Correct: Name mangling just changes the variable name internally; accessing it is still a simple, direct operation.
Understanding how name mangling works and its performance helps you explain how Python handles private variables clearly and confidently.
"What if the method accessed the mangled variable inside a loop running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand double underscore prefix
Attributes starting with double underscores trigger name mangling in Python classes.Step 2: Effect of name mangling
Python adds the class name before the attribute name to avoid accidental access or conflicts.Final Answer:
It adds the class name before the attribute name to avoid conflicts. -> Option AQuick Check:
Name mangling = adds class name prefix [OK]
- Thinking double underscores make attribute public
- Confusing name mangling with deleting attributes
- Assuming attribute name changes to uppercase
Solution
Step 1: Identify private attribute syntax
Private attributes use double underscores at the start of the name, like __my_attr.Step 2: Check correct assignment
Assigning with self.__my_attr = 5 correctly defines a private attribute with name mangling.Final Answer:
self.__my_attr = 5 -> Option AQuick Check:
Double underscore prefix = private attribute [OK]
- Using single underscore instead of double
- Placing underscores after attribute name
- Defining private attribute as a method incorrectly
class A:
def __init__(self):
self.__x = 10
a = A()
print(hasattr(a, '__x'))
print(hasattr(a, '_A__x'))Solution
Step 1: Check attribute __x existence
Due to name mangling, __x is stored as _A__x internally, so hasattr(a, '__x') returns False.Step 2: Check mangled attribute _A__x existence
hasattr(a, '_A__x') returns True because this is the mangled name storing the value.Final Answer:
False True -> Option BQuick Check:
__x hidden as _A__x = False, True [OK]
- Assuming __x is directly accessible
- Confusing mangled name with original
- Expecting both hasattr calls to be True
class B:
def __init__(self):
self.__val = 5
b = B()
print(b.__val)Solution
Step 1: Understand name mangling effect
__val is mangled to _B__val internally, so b.__val does not exist.Step 2: Accessing __val causes AttributeError
Trying to print b.__val raises AttributeError because the attribute is hidden by name mangling.Final Answer:
AttributeError because __val is name mangled -> Option DQuick Check:
Accessing __val directly = AttributeError [OK]
- Expecting no error when accessing __val
- Thinking double underscores cause syntax error
- Confusing AttributeError with TypeError
class C:
def __init__(self):
self.__data = 42
def get_data(self):
return self.__data
c = C()How can you access the private attribute __data from outside the class without using the get_data method?
Solution
Step 1: Understand name mangling for __data
The attribute __data is stored internally as _C__data due to name mangling.Step 2: Access mangled attribute directly
You can access it from outside the class using c._C__data.Final Answer:
c._C__data -> Option CQuick Check:
Access private with _ClassName__attr [OK]
- Trying to access c.__data directly
- Using single underscore _data instead
- Confusing method call with attribute access
