Python Staticmethod Classmethod

Source: Internet
Author: User
Tags instance method
#-*-Coding:utf-8-*-class A (object): Def im (self): "" "instance method: The generic method defined in the Pthon class is an instance method, which has an essential parameter
        , self, which must be in the first position of the argument list, equivalent to the class that the method belongs to in the instantiated instance itself, (Python2 is an instance of the class, Python3 can make anything), and it can only invoke A () through an instance of the Class. IM () : return: "" Print ' Instance methods: ', self @classmethod def cm (CLS): "" class method: Need to
        To an adorner (@classmethod), convert a normal method to a class method, which takes an implied argument as the first argument, which represents the class to which the incoming modification method belongs.
        If the Classmethod is invoked in a derived class, the implicitly-passed argument is the subclass object, not the parent class. : return: "" Print ' Class methods: ', CLS @staticmethod def SM (): "" static method: Requires an adorner ( @staticmethod), converts a normal method to a static method: its arguments are any custom parameters, and of course there are no parameters, it does not require an implied parameter, it can be invoked directly by the class A.sm (), or it can be invoked on the instance, A (). SM () and instance is ignored, the actual or used class calls: return: "" "" Print ' Static method ' class B (A): pass If __name__ =
 = ' __main__ ': a = A () a.im () a.cm () A.sm () print ' =========== ' B = B () b.im () b.cm ()   B.sm ()
 
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.