为什么type.BuiltinFunctionType与BuiltinMethodType相同,即使函数和方法之间显然存在差异?

时间:2018-09-29 09:01:39

标签: python function methods

由于types.BuiltinFunctionTypetypes.BuiltinMethodType完全相同:

>>> import types
>>> types.BuiltinFunctionType is types.BuiltinMethodType
True

不可能将函数与C中实现的方法区分开:

>>> isinstance(sorted, types.BuiltinMethodType)
True
>>> isinstance(int.from_bytes, types.BuiltinFunctionType)
True

但是,python清楚地知道sorted是一个函数,而int.from_bytes是一个方法,我们可以在他们的repr中看到它:

>>> sorted
<built-in function sorted>
>>> int.from_bytes
<built-in method from_bytes of type object at 0x7fded4c9d860>

为什么BuiltinFunctionTypeBuiltinMethodType是同一件事? C中定义的函数和方法之间是否存在区别?

0 个答案:

没有答案