這是因爲在Python中 「隱藏」 的變量獲得存儲方式不同。那裏有一點魔力。
這就是爲什麼它不工作的例子:
class Base(object):
__flag = 'base'
_other_flag = 'base'
def __init__(self) :
pass
@classmethod
def flag(self):
return self.__flag
@classmethod
def other_flag(self):
return self._other_flag
class Derived(Base):
__flag = 'derived'
_other_flag = 'derived'
print 'base flag', Base.flag()
print 'derived flag', Derived.flag()
print 'base other flag', Base.other_flag()
print 'derived other flag', Derived.other_flag()
# Note the following 2 statements:
print 'base flag property', Derived._Base__flag
print 'derived flag property', Derived._Derived__flag
print 'base other flag property', Base._other_flag
print 'derived other flag property', Derived._other_flag
正如你可以在底部看到,它是存儲在一個不同的變量,默默地翻譯成該Base.flag
方法中。
哦,就這麼簡單!非常感謝你! –
不用擔心男人:) –