Python class and static methods, python static
This article describes python's class and static methods. 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.
Copy codeThe 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.
Copy codeThe 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.