Skip to content

Instantly share code, notes, and snippets.

@IuryAlves IuryAlves/name_mangling.py
Last active Dec 11, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.