Python classmethod() 函数用法
2023-09-17 22:26:59
classmethod()
方法将方法转换为类方法。
建议使用@classmethod decorator而不是classmethod()
语法:
classmethod(function)
参数:
函数:要转换为类方法的函数。
返回值:
返回类方法。
下面的示例演示classmethod()
方法的工作。
class student:
name = 'John'
def show_name(self):
print("The student's name is ",self.name)
student.show_name = classmethod(student.show_name)
student.show_name()
输出:
The student's name is John