, , __getattribute__, . , , : , , , self.isValid __getattribute__ , - (type(self).isValid(self) , , , ).
: ", , , classmethods. , , __getattribute__ (, - , ), , , ).
: State: . :.
class _NotValid(object):
def isValid(self):
return False
def setValid(self, yesno):
if yesno:
self.__class__ = TheGoodOne
class TheGoodOne(object):
def isValid(self):
return True
def setValid(self, yesno):
if not yesno:
self.__class__ = _NotValid
setValid , __class__ , - , __class__ - , , , , , . , , " ", , .
__getattribute__ , (, , ;-), :
class _Valid(object):
def __init__(self, actualobject):
self._actualobject = actualobject
class Wrapit(object):
def __init__(self):
self._themethods = _Valid(self)
def isValid(self):
return False
def __getattr__(self, n):
if self.isValid():
return getattr(self._themethods, n)
raise AttributeError(n)
, __getattribute__, , __getattr__ , - () __dict__, ; . _Valid self._actualobject, - ( , , Q, , , ). , __getattribute__, ( ).
, , . , weakref Python, - , , , .
(, _actualobject _Valid , _themethods).