Skip to content

Instantly share code, notes, and snippets.

@gennad
Created June 1, 2012 10:43
Show Gist options
  • Save gennad/2851103 to your computer and use it in GitHub Desktop.
Save gennad/2851103 to your computer and use it in GitHub Desktop.
Tricky question Python
class A:
def __init__(self):
# self = B instance, when mangled, __value is replaced
# by _CurrentClass__value , so self.__dict__['_A__value'] = 1
self.__value = 1
self.__a = 123
def getvalue(self):
print self.__a # OK
return self.__value # Error - because of assignment in B.__init__
class B( A ):
def __init__(self):
A.__init__(self)
self.__value = 2 # self.__dict__['_B__value'] = 2
b = B()
print b.getvalue() == b.__value # Can't reference __value in b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment