【发布时间】:2017-09-24 03:33:08
【问题描述】:
考虑这个例子:
>>> class Bar(object):
...
... def __init__(self, name):
... self.name = name
... def __set__(self, instance, value):
... setattr(instance, self.name, value)
... def __get__(self, instance, owner):
... return getattr(instance, self.name, owner)
...
>>> class Foo(object):
... bat = Bar('bat')
...
>>> Foo.bat
<class 'Foo'>
>>> type(Foo.bat)
<class 'type'> # how would you get <class 'Bar'> ?
我想写一些pytests 断言正确的描述符已分配给正确的属性。
但是一旦分配了描述符,我似乎无法检查它的类型
【问题讨论】:
-
为什么
foo.bat不会导致 StackOverflow 错误? -
@MadPhysicist:
Foo.bat将None作为instance传递给__get__。
标签: python descriptor