Skip to content

Instantly share code, notes, and snippets.

@joshkunz
Created March 30, 2015 15:58
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 joshkunz/fce5e5daf1a63b47435a to your computer and use it in GitHub Desktop.
Save joshkunz/fce5e5daf1a63b47435a to your computer and use it in GitHub Desktop.
Python name mangling fun
class foo:
__local = 10
def local(self): return self.__local
def test(self, bar): return self.__local, bar.__local
class empty:
pass
ei= empty()
ei.__local = "hello from empty"
f = foo()
print("First local:", f.local())
print("empty local:", ei.__local)
print("both:", f.test(ei))
$ python3 ex.py
First local: 10
empty local: hello from empty
Traceback (most recent call last):
File "ex.py", line 14, in <module>
print("both:", f.test(ei))
File "ex.py", line 4, in test
def test(self, bar): return self.__local, bar.__local
AttributeError: 'empty' object has no attribute '_foo__local'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment