Last active
December 20, 2015 14:38
-
-
Save Erreinion/6147743 to your computer and use it in GitHub Desktop.
Example of 'private' function in Python. _ = don't expose, __ = hide.
http://stackoverflow.com/a/70900/1060378
This file contains hidden or 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(object): | |
def __init__(self): | |
self.__baz = 42 | |
def foo(self): | |
print self.__baz | |
class Bar(Foo): | |
def __init__(self): | |
super(Bar, self).__init__() | |
self.__baz = 21 | |
def bar(self): | |
print self.__baz | |
>>> x = Bar() | |
>>> x.foo() | |
42 | |
>>> x.bar() | |
21 | |
>>> print x.__dict__ | |
{'_Bar__baz': 21, '_Foo__baz': 42} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment