Name Mangling in Python— Attributes That Shouldn’t Be Accessed Directly

Liu Zuo Lin
Level Up Coding
Published in
3 min readApr 26, 2024

--

class Dog:
def __init__(self, name, age):
self.name = name
self.age = age

dog = Dog('rocky', 5)

print(dog.name) # rocky
print(dog.age) # 5

^ here we have a normal Python Dog class with 2 attributes .name and .age — if we attempt to access these attributes directly by using dog.name and dog.age, we can do so without problem

--

--