Skip to content

Instantly share code, notes, and snippets.

@ychennay
Last active March 19, 2020 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ychennay/d522ed7d29fddd4673992446ce6e4109 to your computer and use it in GitHub Desktop.
Save ychennay/d522ed7d29fddd4673992446ce6e4109 to your computer and use it in GitHub Desktop.
name mangling
class Person:
def __init__(self, name):
self.__secret_attribute = 42
self.name = name
if __name__ == "__main__":
person = Person("Yu")
# print(person.__secret_attribute) # AttributeError: 'Person' object has no attribute '__secret_attribute'
# notice that a name mangled attribute has been added to this instance's __dict__
print(person.__dict__) # {'_Person__secret_attribute': 42, 'name': 'Yu'}
# we can still access the secret attribute
print(person.__getattribute__("_Person__secret_attribute")) # 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment