gpt4 book ai didi

python - 什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是一个?

转载 作者:太空狗 更新时间:2023-10-29 17:33:28 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com