Python 类方法装饰器@classmethod
Python 类方法装饰器@classmethod
在 Python 中,@classmethod装饰器用于将类中的一个方法声明为可以使用ClassName.MethodName()调用的类方法。 类方法也可以使用类的对象来调用。
@classmethod是 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 "<pyshell#22>", line 1, in <module> Student.tostring() File "<pyshell#21>", 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 vs @staticmethod
下表列出了类方法和静态方法的区别:
@classmethod | @staticmethod |
---|---|
声明一个类方法。 | 声明一个静态方法。 |
它可以访问类属性,但不能访问实例属性。 | 它不能访问类属性或实例属性。 |
可以使用ClassName.MethodName()或object.MethodName()来调用。 | 可以使用ClassName.MethodName()或object.MethodName()来调用。 |
它可以用来声明返回类对象的工厂方法。 | 它不能返回类的对象。 |
评论关闭