这是关于类方法的调用。类方法是绑定到类而不是实例的方法,因此它们不需要实例化即可使用。该语法表示对类方法的调用,并将 obj2 作为该方法的第一个参数传递。然后,返回一个新的可调用对象,该对象可以用 obj1 调用,并等效于对类方法的调用。
示例代码:
class MyClass:
@classmethod
def my_class_method(cls, x):
print("Class method called with x =", x)
return x
# 基于类调用类方法
result = MyClass.my_class_method(5)
print(result) # 输出:Class method called with x = 5,5
# 包含 obj1, obj2 参数的类方法调用
obj1 = MyClass()
obj2 = 8
callable_method = MyClass.my_class_method(obj2)
result = callable_method(obj1)
print(result) # 输出:Class method called with x = 8,8