由于types.BuiltinFunctionType
与types.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>
为什么BuiltinFunctionType
和BuiltinMethodType
是同一件事? C中定义的函数和方法之间是否存在区别?