This article mainly introduces the class and static methods of python, and analyzes the implementation skills and application methods of class and static methods in Python in the form of examples, for more information about python class and static methods, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
Python does not have the static keyword in C ++. what is its static method? Is there a rare kind of method in other languages as a magic horse?
Static methods and class methods in python depend on python modifiers.
The code is as follows:
Class MyClass:
Def method (self ):
Print ("method ")
@ Staticmethod
Def staticMethod ():
Print ("static method ")
@ Classmethod
Def classMethod (cls ):
Print ("class method ")
Have you noticed that the common object methods, class methods, and static methods are different?
Object methods have self parameters, and class methods have cls parameters. static methods do not need these additional parameters.
In C ++, there is no concept of class methods.
The code is as follows:
Class A (object ):
"This ia A Class"
@ Staticmethod
Def Foo1 ():
Print ("Call static method foo1 () \ n ")
@ Classmethod
Def Foo2 (cls ):
Print ("Call class method foo2 ()")
Print ("cls. _ name _ is", cls. _ name __)
A. Foo1 ();
A. Foo2 ();
The result is:
Call static method foo1 ()
Call class method foo2 ()
Cls. _ name _ is
I hope this article will help you with Python programming.