Skip to content

Instantly share code, notes, and snippets.

@IuryAlves
Last active December 11, 2016 23:41
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 IuryAlves/39d82caf836af1a9b6442e393919d0e8 to your computer and use it in GitHub Desktop.
Save IuryAlves/39d82caf836af1a9b6442e393919d0e8 to your computer and use it in GitHub Desktop.
Simple example to explain name mangling in python.
# 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