Python @staticmethod, @classmethod, @property

Source: Internet
Author: User

@staticmethod, @classmethod, @property usage and function

classFoo (object):def __init__(self): Self._name="Property Test"        Print "Init"    defTest (self):Print "class Method"@propertydefname (self):returnself._name @staticmethoddefStaticfun ():Print "static method function"@classmethoddefClassfun (CLS):Print "class method function"if '__main__'==__name__: Foo.staticfun () Foo.classfun () F=Foo ()Printf.name f.test ()

    • @staticmethod: Static methods
    • @classmethod: Class method
    • @property: Calling a function as a class member property

In C + +, a function called directly by a class name is known as a static method, there is no difference between @staticmethod and @classmethod, and of course there are some subtle differences, @classmethod parameters that require an implicit pass-through class name.

Of course, unlike in C + +, a member method in C + + cannot be called by an instantiated object of a class, directly through the class name.

Python @staticmethod, @classmethod, @property

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.