当我尝试使用 super(type, obj) 创建绑定类方法时,有些事情有所不同

When I try to use super(type, obj) to create a bound classmethod, there are some things different

我输入了这样的代码

class A:
    @classmethod
    def m1(cls):
        pass

class B(A):
    @classmethod
    def m1(cls):
        print(super(B, B).m1)

然后我调用了B.m1,结果是

<bound method A.m1 of <class '__main__.B'>>

根据 super 的文档,如果使用第二个参数,super 将 return 绑定对象,代码显示它有效。

我知道method__new__也是一个classmethod,所以我敲了一个这样的测试代码

class A(object):
    def __new__(cls):
        print(super(A, A).__new__)

我还以为会和前一个一样呢。但结果让我很困惑:

<built-in method __new__ of type object at 0x103e33cf0>

然后我转向方法 __init__ 是这样的:

class A(object):
    def __init__(self):
        print(super(A, self).__init__)

结果是

<method-wrapper '__init__' of A object at 0x104f59da0>

这与实例的绑定对象不同。

class A:
    def m1(self):
        pass

class B(A):
    def m1(self):
        print(super(B, self).m1)

结果是

<bound method A.m1 of <__main__.B object at 0x104f59da0>>

所以我想知道为什么内置对象和自定义对象的结果不同?

命名。而已。您正在处理 C 代码中定义的方法(描述符)与 Python 代码中定义的方法。

没有功能差异,真的没有必要担心实现差异。