Python example methods, class methods, static methods and the difference between the call relationship

Source: Internet
Author: User

If you look at this graph only, many people may look at the confused, especially those who have studied the completely object-oriented language, Python is dual-oriented, can be either oriented to function programming or object-oriented programming, so-called function-oriented is a separate one. PY file, there is no class, all are some functions, call the time of the import module, through the module name. function name () can be called, completely do not need the class, then you may ask, what kind of hair? Classes are used for object-oriented programming, classes can have their own properties, classes can create many instances, each instance can have different properties, which also preserves a lot of private data, in short, there is a need.

Looking at this diagram above, the function defined in the class is the method, the class method needs @ Classmethod modification and has a hidden parameter cls, the instance method must have a parameter self, the static method must have @staticmethod adornment, the class and instance can access the static method, Instances can access instance methods, and class methods can be accessed, classes can access class methods, and instance methods can be accessed, and access to instance methods must take parameter self, which can be understood as an instance of a class that accesses instance methods without parameters. The class itself can access the function, but not the instance.

Python example methods, class methods, static methods and the difference between the call relationship

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.