什么是wrapper_descriptor,为什么在这种情况下Foo .__ init __()是一个? [英] What is a wrapper_descriptor, and why is Foo.__init__() one in this case?
问题描述
import inspect
class Foo(object):
pass
if __name__ == '__main__':
print type(Foo.__init__)
print inspect.getsourcelines(Foo.__init__)
输出:
<type 'wrapper_descriptor'>
Traceback (most recent call last):
*snip*
File "/usr/lib/python2.7/inspect.py", line 420, in getfile
'function, traceback, frame, or code object'.format(object))
TypeError: <slot wrapper '__init__' of 'object' objects> is not a module, class, method, function, traceback, frame, or code object
Google搜寻几乎没有提供有关wrapper_descriptor到底是什么的信息,以及为什么空类具有__init__
方法而不是方法,而是一个wrapper_descriptor的有用信息.
Googling gives very little useful information about what, exactly, a wrapper_descriptor is, and why an empty class has an __init__
method that is not a method, but rather a wrapper_descriptor.
这到底是怎么回事?是否所有没有__init__
方法的类都具有以下wrapper_descriptor之一?为什么在类字典中根本没有__init__
?
What exactly is going on here? Do all classes without __init__
methods have one of these wrapper_descriptor things? Why is there an __init__
in the class dict at all?
推荐答案
您遇到的是实现细节.对于
What you've run into is an implementation detail. This is pretty typical for classes implemented in C, as object
is. It's not a Python method, it is a C method, and the wrapper is part of this interface.
为什么类字典中根本没有
__init__
?
它不在类字典中,而在object
字典中. object
具有__init__
,因此当您尝试使用super()
调用类的基类的__init__
方法时,它不会失败.
It's not in the class dict, it's in the object
dict. object
has an __init__
so that when you try to call your class's base classes' __init__
methods using super()
, it doesn't fail.
这篇关于什么是wrapper_descriptor,为什么在这种情况下Foo .__ init __()是一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!