Last active
December 11, 2016 23:41
-
-
Save IuryAlves/39d82caf836af1a9b6442e393919d0e8 to your computer and use it in GitHub Desktop.
Simple example to explain name mangling in python.
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
# coding: utf-8 | |
class Image(object): | |
def __format(self): | |
return 'PNG' | |
image = Image() | |
try: | |
image.__format() # will raise AttributeError | |
except AttributeError as exc: | |
print(exc) | |
print(dir(image)) # but the format method exists with another name | |
format_ = getattr(image, '_Image__format')() | |
print(format_) # PNG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment