Python Classmethod modifier

Source: Internet
Author: User
Tags python classmethod

The function that corresponds to the classmethod modifier does not need to be instantiated and does not require the self parameter , but the first parameter needs to be a cls parameter that represents its own class, which can be used to invoke the properties of the class , the methods of the class , the instantiation of objects , and so on. Returns the class method.

Instance:

1 #!/usr/bin/python2 #-*-coding:utf-8-*-3  4 classA (object):5Bar = 16     deffunc1 (self):7         Print 'Foo'  8 @classmethod9     defFunc2 (CLS):Ten         Print 'Func2' One         PrintCls.bar ACLS (). FUNC1 ()#Call the Foo method -   -A.FUNC2 ()#No instantiation required

Output:

1 Func2 2 13 foo

Python Classmethod modifier

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.