The methods in a class are associated with the objects created for it. For invoking the methods defined inside the class, the presence of an instance is necessary. The classmethod is a method that is also defined inside the class but does not need any object to invoke it. A classmethod can be invoked using the name of the class. For making a method to a class method, the function decorator "classmethod" receives the class as the implicit first argument using "cls" argument. This is similar to the methods receiving object as the first argument using the "self" argument. The syntax for using classmethod is shown below.
As shown in the above syntax, any method can be made into classmethod by adding "@classmethod" decorator before the definition of the method. Classmethod is bound to class but not to objects.
In the above code, the constructor consists of assigning values for attributes "name" and "age". The function to convert year to age is defined and converted to classmethod using "@classmethod". The cls() function in the return statement of the class method from_birth_year invokes the constructor of the class Person by creating the object. Here, the object name is taken care by the interpreter and the created object is returned. The statement "cls(name, date.today().year-year)" creates an object for the class (which is received as the first argument of the classmethod), and its first argument corresponds to the name and the second attribute finds the difference between the current year and the year which is passed on to the method. The class method returns a cls() method, which is an object of the class.
Now an object is created for the class Person and called with two parameters, "Mayank" and 21. The instance variables name and age are assigned with the values, respectively.
The above code can also be done using the classmethod from_birth_year as follows.
Static Method
Static method is similar to a class method, which can be accessed without an object. A static method is accessible to every object of a class, but methods defined in an instance are only able to be accessed by that object of a class. Static methods are not allowed to access the state of the objects (attributes of the class). The syntax for the static method is as follows.
In the above syntax, the method is converted to static method by having "@staticmethod" before the method definition. Also, "self" is not associated with the argument list of the static method.
In the above code, the "is_adult()" method is defined and converted to a static method that returns true, if the given argument is greater than 18 or returns false. Note that the function "is_adult()" does not have any "self" argument, and also, the function is not defined in such a way that it alters the state of the object.
In the above method, the created object with 'Mayank' and 21 shows 'True' on calling with the method is_adult. Unlike the above, the method is_adult can be invoked without creating an object.
The differences between classmethod and staticmethod are shown below.