当我尝试嵌套描述符/装饰器时,我很难理解会发生什么。我正在使用python 2.7。
例如,让我们采用 和 的以下简化property版本classmethod:
class MyProperty(object):
def __init__(self, fget):
self.fget = fget
def __get__(self, obj, objtype=None):
print 'IN MyProperty.__get__'
return self.fget(obj)
class MyClassMethod(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype=None):
print 'IN MyClassMethod.__get__'
def f(*args, **kwargs):
return self.f(objtype, *args, **kwargs)
return f
试图嵌套它们:
class A(object):
# doesn't work:
@MyProperty
@MyClassMethod
def klsproperty(cls):
return 555
# works:
@MyProperty
def prop(self):
return 111
# works:
@MyClassMethod
def klsmethod(cls, x):
return x**2
% print A.klsproperty
IN MyProperty.__get__
...
TypeError: 'MyClassMethod' object is not callable
__get__内部描述符的方法MyClassMethod没有被调用。由于无法弄清楚原因,我尝试输入(我认为是)一个无操作描述符:
class NoopDescriptor(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype=None):
print 'IN NoopDescriptor.__get__'
return self.f.__get__(obj, objtype=objtype)
尝试在嵌套中使用无操作描述符/装饰器:
class B(object):
# works:
@NoopDescriptor
@MyProperty
def prop1(self):
return 888
# doesn't work:
@MyProperty
@NoopDescriptor
def prop2(self):
return 999
% print B().prop1
IN NoopDescriptor.__get__
IN MyProperty.__get__
888
% print B().prop2
IN MyProperty.__get__
...
TypeError: 'NoopDescriptor' object is not callable
我不明白为什么B().prop1有效而B().prop2无效。
问题:
- 我究竟做错了什么?为什么我会收到
object is not callable错误消息? - 什么是正确的方法?
MyClassProperty例如,在重用MyClassMethod和MyProperty(或classmethod和property)时定义的最佳方法是什么