从另一个类中借用实例方法的实现很简单,但是如何使用类方法呢?
class A:
def im(self):
print(self.__class__.__name__)
@classmethod
def cm(cls):
print(cls.__name__)
class B:
im = A.im
cm = A.cm # line X
classmethod(cm)
B().im() # B - OK
B.cm() # A - not what I want
我还尝试将 X 行更改为cm = A.cm.__func__
,这导致TypeError: cm() missing 1 required positional argument: 'cls'