A classmethod
对象是描述符。你需要了解描述符是如何工作的。
概括地说,描述符是具有方法__get__
,它有三个参数的对象:self
,一个instance
和instance type
。
在正常属性查询,如果查到的对象A
有一个方法__get__
,该方法被调用和它返回的地方被替换为对象A
。这就是函数(也是描述符)在对象上调用方法时如何成为绑定方法。
class Foo(object):
def bar(self, arg1, arg2):
print arg1, arg2
foo = Foo()
# this:
foo.bar(1,2) # prints '1 2'
# does about the same thing as this:
Foo.__dict__['bar'].__get__(foo, type(foo))(1,2) # prints '1 2'
A classmethod
对象的工作方式相同。当它被抬起时,它的__get__
方法被调用。类方法的__get__
丢弃对应于instance
(如果有的话)的参数,并且当它在包装的函数上调用__get__
时仅传递instance_type
。
一个说明性的涂鸦:
In [14]: def foo(cls):
....: print cls
....:
In [15]: classmethod(foo)
Out[15]: <classmethod object at 0x756e50>
In [16]: cm = classmethod(foo)
In [17]: cm.__get__(None, dict)
Out[17]: <bound method type.foo of <type 'dict'>>
In [18]: cm.__get__(None, dict)()
<type 'dict'>
In [19]: cm.__get__({}, dict)
Out[19]: <bound method type.foo of <type 'dict'>>
In [20]: cm.__get__({}, dict)()
<type 'dict'>
In [21]: cm.__get__("Some bogus unused string", dict)()
<type 'dict'>
上描述更多信息可以在这里找到(在其他地方): http://users.rcn.com/python/download/Descriptor.htm
为了得到由classmethod
包裹函数名的特定任务:
In [29]: cm.__get__(None, dict).im_func.__name__
Out[29]: 'foo'
我对“缺少”'__name__'没有深入的了解,但要确保类方法有一个'__call__'包装。 – mjv 2009-11-05 00:29:14