为什么会这样?在
class IsInstanceScrewer(object):
def __init__(self, value):
self.value = value
def __getattribute__(self, name):
if name in ('value',):
return object.__getattribute__(self, name)
value = object.__getattribute__(self, 'value')
return object.__getattribute__(value, name)
isinstance(IsInstanceScrewer(False), bool) #True
isinstance(IsInstanceScrewer([1, 2, 3]), list) #True
该类绝对不是bool的实例,即使它试图包装它。在
__getattribute__
返回包装值的__class__
,而不是它自己的__class__
:这可能是你想要的行为,也可能不取决于你在做什么。在
相关问题 更多 >
编程相关推荐