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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!