Name Mangling
Often hear this term from Compiler world.
Name mangling is a technique used by compilers to generate unique symbolic names for functions, variables, and other identifiers.
Name mangling solves the problem of overloaded identifiers in programming languages.
TODO: Check what happens on the assembly code when you inspect with objdump
int add(int a, int b); // Mangles to something like _Z3addii
float add(float a, float b); // Mangles to something like _Z3addff
Python
For “private” member variables (those that start with __
), Python mangles the name to _ClassName__variable
.
class MyClass:
def __init__(self):
self.__private_var = 10
obj = MyClass()
# Accessing the variable directly would raise an AttributeError
# obj.__private_var # This will raise an error
# Accessing through the mangled name works
print(obj._MyClass__private_var) # Output: 10