在 Python 中,@classmethod装饰器用于将类中的一个方法声明为可以使用ClassName.MethodName()调用的类方法。 类方法也可以使用类的对象来调用。

创新互联服务项目包括珠海网站建设、珠海网站制作、珠海网页制作以及珠海网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,珠海网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到珠海省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
@classmethod是 classmethod() 函数的替代函数。建议使用@classmethod修饰器代替函数,因为它只是一个语法糖。
cls,可以用来访问类属性。ClassName.MethodName()和对象调用类方法。下面的示例声明了一个类方法。
Example: @classmethod
class Student:
name = 'unknown' # class attribute
def __init__(self):
self.age = 20 # instance attribute
@classmethod
def tostring(cls):
print('Student Class Attributes: name=',cls.name) 上图中,Student类包含一个类属性name和一个实例属性age。 tostring()方法用@classmethod装饰器装饰,使其成为类方法,可以使用Student.tostring()调用。 注意,任何类方法的第一个参数必须是cls,可以用来访问类的属性。您可以给第一个参数起任何名字,而不是cls。
现在,您可以使用类方法,如下所示。
Example: Access Class Method
>>> Student.tostring()
Student Class Attributes: name=unknown 但是,也可以使用对象调用相同的方法。
Example: Calling Class Method using Object
>>> std = Student()
>>> std.tostring()
Student Class Attributes: name=unknown
>>> Student().tostring()
Student Class Attributes: name=unknown 类方法只能访问类属性,而不能访问实例属性。如果试图访问类方法中的实例属性,将会引发错误。
Example: @classmethod
class Student:
name = 'unknown' # class attribute
def __init__(self):
self.age = 20 # instance attribute
@classmethod
def tostring(cls):
print('Student Class Attributes: name=',cls.name,', age=', cls.age) Example: Access Class Method
>>> Student.tostring()
Traceback (most recent call last):
File "", line 1, in
Student.tostring()
File "", line 7, in display
print('Student Class Attributes: name=',cls.name,', age=', cls.age)
AttributeError: type object 'Student' has no attribute 'age' 类方法也可以用作工厂方法来获取类的对象,如下所示。
Example: @classmethod
class Student:
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age # instance attribute
@classmethod
def getobject(cls):
return cls('Steve', 25) 下面调用类方法来获取对象。
Example: Class Method as Factory Method
>>> std = Student.getobject()
>>> std.name
'Steve'
>>> std.age
25
下表列出了类方法和静态方法的区别:
| @classmethod | @staticmethod |
|---|---|
| 声明一个类方法。 | 声明一个静态方法。 |
| 它可以访问类属性,但不能访问实例属性。 | 它不能访问类属性或实例属性。 |
可以使用ClassName.MethodName()或object.MethodName()来调用。 | 可以使用ClassName.MethodName()或object.MethodName()来调用。 |
| 它可以用来声明返回类对象的工厂方法。 | 它不能返回类的对象。 |