Created
June 1, 2012 10:43
-
-
Save gennad/2851103 to your computer and use it in GitHub Desktop.
Tricky question Python
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 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