Describe
The function that corresponds to the Classmethod modifier does not need to be instantiated, does not require the self parameter, but the first parameter needs to be a CLS parameter that represents its own class, can invoke the class's properties, the class's methods, instantiate the object, and so on.
Grammar
Classmethod Syntax:
Classmethod
Parameters
No
return value
Returns the class method of the function.
Instance
The following examples show how Classmethod is used:
#!/usr/bin/python
#-*-Coding:utf-8-*-
Class A(object):
bar = 1 def
< Span class= "Hl-identifier" >func1 (self : print ( "foo ' )
< Span class= "Hl-identifier" >< Span class= "Hl-code" > @ classmethod
< Span class= "hl-brackets" > def func2 (cls) :
print (" func2 /span>
print (cls. Bar
CLS(). Func1() # call Foo method A. Func2() # No instances required
The output is:
Func21foo
Python Classmethod modifier