python static methods, class methods, properties, methods, examples of the method

DAY 3. static methods, class methods, properties, methods, examples of the method

There are four methods, instance methods, class methods, static methods, properties, methods

  • Examples of methods

The first parameter is an instance method self, he would point to an instance of an object class, it can only be called an object, such as

class Demo:

    def instanceMethod(self):
        print("this is a instance method")

if __name__ == "__main__":
    demo = Demo()
    demo.instanceMethod()

When the incoming call does not need to point the object with the parameters, Python will call object instance method as the first parameter passed example of a method equivalent to

if __name__ == '__main__':
    demo = Demo()
    Demo.instanceMethod(demo)
  • Class Methods

Use decorator @classmethod. The first argument must be a class object is the current, this parameter is generally agreed to name "cls", may be used (e.g. of Cf ()) or an instance (e.g., C (). F ()) calling a class method. In addition to the classes, the instance is ignored. If the derived class call the class method, the derived class object is passed as the first argument implied.

class Demo:

    @classmethod
    def classMethod(self):
        print("this is a class method")

if __name__ == "__main__":
    demo = Demo()
    Demo.classMethod()
    demo.classMethod()

Also syntactic sugar, the class or object will automatically call the class method as the first argument is invoked with the point

  • Static method

Methods @staticmethod decorated with no self parameter called static method, static method does not receive an implicit first parameter, c ++ static method is similar, but takes up the namespace classes, no contact with the class, the a class name or object name calling

  • Properties Methods

The method used to become a static property, use decorator@property

class C:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

TODO: a bit difficult to learn another day special

to sum up

method Decorator transfer Usage scenarios (personal understanding)
Examples of methods / It can only be called an object Most scenes
Class Methods @classmethod Object or class can be invoked Operate only class attribute
Static method @staticmethod Object or class can be invoked It does not operate in the class attributes and methods
Properties Methods @property By calling the static method attribute (without brackets) EVERYTHING

Reference article
GitHub face questions about python

Python static methods, class methods, properties, methods

Python object-oriented method, class methods, properties, methods

Python distinguished role instance method, class methods, static methods

python documentation staticmethod

python documentation classmethod

发布了62 篇原创文章 · 获赞 33 · 访问量 1万+

Guess you like

Origin blog.csdn.net/zjbyough/article/details/95778530