A detailed description of Python's class methods, common methods and static methods

Source: Internet
Author: User
Tags instance method

First, the formal difference, the instance method implies the class instance self, and the class method implies that the class itself is CLS. Static methods have no implicit arguments, and they can also be called directly to a class instance.

So logically, the class method is called by the class, the instance method is called by the instance, and both of the static methods can be called. The main difference is the difference in parameter passing, where the instance method silently passes the self reference as a parameter, and the class method silently passes the CLS reference as the parameter.

Next see if the class and instance can invoke the Classes property

Therefore, the instance method (ordinary method) should be called by the instance, the class cannot be called;

A class method can be called by a class, and it can be called by an instance because the parameter CLS is passed in.

Static methods can both be called

summed up into a table is:

Note the difference between the result of a class instance method call and the result of a static method call:

Instance methods (common methods) —————————————————————— change as instance properties change

A class method (whether a class call or an instance call) ——————————————— is the value of a class property and does not change with instance properties

Static methods ———————————————————————————— can not access the class properties, so directly output the value of the incoming method

A detailed description of Python's class methods, common methods and static methods

Related Article

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.