Classmethod Decorator
Python Class Method Decorator @classmethod
Section titled “Python Class Method Decorator @classmethod”In Python, the @classmethod decorator is used to declare a method in the class as a class method that can be called using ClassName.MethodName(). The class method can also be called using an object of the class.
@classmethod``ClassName.MethodName()
The @classmethod is an alternative of the classmethod() function. It is recommended to use the @classmethod decorator instead of the function because it is just a syntactic sugar.
@classmethodclassmethod()@classmethod
@classmethod Characteristics
Section titled “@classmethod Characteristics”- Declares a class method.
- The first parameter must be cls, which can be used to access class attributes.
cls- The class method can only access the class attributes but not the instance attributes. - The class method can be called using ClassName.MethodName() and also using object.
ClassName.MethodName()- It can return an object of the class.
The following example declares a class method.
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.tostring() #Student Class Attributes: name=unknown`class Student: name = ‘unknown’ # class attribute def init(self): self.age = 20 # instance attribute
@classmethoddef tostring(cls): print('Student Class Attributes: name=',cls.name)Student.tostring() #Student Class Attributes: name=unknown[Try it](/codeeditor?cid=python-3z8uwbrs9) Above, the Student class contains a class attribute name and an instance attribute age. The tostring() method is decorated with the @classmethod decorator that makes it a class method, which can be called using the Student.tostring(). You can call the class method as classname.method() or using class object object.method(). Studentnameagetostring()@classmethodStudent.tostring()classname.method()object.method()` Note: The first parameter of any class method must be cls that can be used to access the class's attributes. You can give any name to the first parameter instead of cls. `clscls`
The class method can only access class attributes, but not the instance attributes. It will raise an error if trying to access the instance attribute in the class method.
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)
Student.tostring() #calling class method`class Student: name = ‘unknown’ # class attribute def init(self): self.age = 20 # instance attribute
@classmethoddef tostring(cls): print('Student Class Attributes: name=',cls.name,', age=', cls.age)Student.tostring() #calling class method`Try it The class method can also be used as a factory method to get an object of the class, as shown below.
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)
std = Student.getobject()print(std.name) #'Steve'print(std.age) #25`class Student: def init(self, name, age): self.name = name # instance attribute self.age = age # instance attribute
@classmethoddef getobject(cls): return cls('Steve', 25)std = Student.getobject()
print(std.name) #‘Steve’
print(std.age) #25`Try it
@classmethod vs @staticmethod
Section titled “@classmethod vs @staticmethod”The following table lists the difference between the class method and the static method:
static methodClassName.MethodName()``object.MethodName()``ClassName.MethodName()``object.MethodName()