Skip to content

Instantly share code, notes, and snippets.

@Erreinion
Last active December 20, 2015 14:38
Show Gist options
  • Save Erreinion/6147743 to your computer and use it in GitHub Desktop.
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
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