Name Mangling

Double underscore prefix

Interview Relevant: Python internals

Name Mangling

Double underscore triggers name mangling.

Code Examples

Name mangling prevents conflicts.

python
1class Parent:
2    def __init__(self):
3        self.__value = 10  # _Parent__value
4
5class Child(Parent):
6    def __init__(self):
7        super().__init__()
8        self.__value = 20  # _Child__value
9
10c = Child()
11print(c._Parent__value)  # 10
12print(c._Child__value)   # 20

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In