作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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
关于 wrapper_descriptor 到底是什么,以及为什么空类有一个不是方法而是 wrapper_descriptor 的 __init__
方法的有用信息很少。
这里到底发生了什么?没有 __init__
方法的所有类都具有这些 wrapper_descriptor 之一吗?为什么类字典中有一个 __init__
?
最佳答案
您遇到的是实现细节。这对于用 C 实现的类来说非常典型,例如 object
。它不是 Python 方法,它是 C 方法,包装器是此接口(interface)的一部分。
Why is there an
__init__
in the class dict at all?
它不在类字典中,它在 object
字典中。 object
有一个 __init__
这样当你尝试使用 super()
调用你的类的基类的 __init__
方法时, 它不会失败。
关于python - 什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15512183/
import inspect class Foo(object): pass if __name__ == '__main__': print type(Foo.__init__)
我是一名优秀的程序员,十分优秀!