Created
March 30, 2015 15:58
-
-
Save joshkunz/fce5e5daf1a63b47435a to your computer and use it in GitHub Desktop.
Python name mangling fun
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 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)) |
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
$ 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