Last active
March 19, 2020 23:26
-
-
Save ychennay/d522ed7d29fddd4673992446ce6e4109 to your computer and use it in GitHub Desktop.
name mangling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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