Python static Method class method Property method

Source: Internet
Author: User
Tags python static method

Both the static and class member methods of Python can be accessed by a class or instance, and the concepts are not easy to clarify, but there are differences:

1) Static methods do not need to pass in the self parameter, and class member methods need to pass in a CLS parameter representing this class;

2) from 1th, the static method cannot access the instance variable, but the class member method also cannot access the instance variable, but can access the class variable; 3) The static method is somewhat like the Function ToolPak, and the class member method is closer to the static method in the Java object-oriented concept. Static methods
@staticmethod
Def staticmd ():
print ' static method @classmethod
def CLASSMD (CLS):
print ' class method ' static method:Unable to access class properties, instance properties, equivalent to a relatively independent method, and the class actually has nothing to do, in other words, in fact, is placed in the scope of a class function. class member methods:You can access class properties and cannot access instance properties. Property Method:@property Python's built-in @property decorator is responsible for turning a method into a property call using the properties decorator, which allows the member function to be called read-only and does not provide setter and deleter
class C (object):     def __init__ (self):         = None    @property    def  x (self):        "" ","I ' m the ' X ' property. """        return self._x    @x.setter    def  x (Self, value):        = value    @ X.deleter    def  x (self):        del self._x

Python static Method class method Property method

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.