Python's class method and static method _python

Source: Internet
Author: User
Tags in python

The examples in this article describe Python's class and static methods. Share to everyone for your reference. The specific analysis is as follows:

Python does not have the static keyword in C + +, and how does it statically approach it? And what are some of the rare classes in other languages that are God's horse?

The implementation of static and class methods in Python is dependent on the Python modifiers.

Copy Code code as follows:
Class MyClass:

Def method (self):
Print ("Method")

@staticmethod
Def staticmethod ():
Print ("static method")

@classmethod
def classmethod (CLS):
Print ("class method")

Are you aware of common object methods, class methods, and static methods?
The object method has the self argument, and the class method has a CLS parameter, which is not required by the static method.
In C + + there is no class method with a conceptual

Copy Code code as follows:

Class A (object):
"This ia A Class"

@staticmethod
Def Foo1 ():
Print (called 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:
Call static method Foo1 ()

Call class Method Foo2 ()
CLS.__NAME__ is A

I hope this article will help you with your 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.