Python class and static methods

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.