Method python static class instance method Method

Class Method 1

@classmethod
will to a method of packaging a class method.

A class class method to itself as the first argument, as an example of a method to Example themselves as the first argument. Please use the following habits to declare class methods:

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

decorator form such @classmethod called function - For details, see function definition.
class How to law of Tone use ( C . f ( ) ) ( C ( ) . f ( ) ) \ Color {red} {class calls the method may be performed on the class (e.g. of Cf ()) may be performed on instances (e.g., C (). F ()). }
Examples of its kind outside of their class will be ignored. If the method invocation on its derived class belongs to the class, the derived class object is passed as the first argument is implicit.
C ++ Java class methods and static methods or different. If you need the latter, see staticmethod ().

2 static method

@staticmethod
transfer methods as static methods.

\ Color {red} static method does not receive an implicit first parameter. To declare a static method, use this syntax

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

decorator form such @staticmethod called function - For details, see function definition.

( C . f ( ) ) ( C ( ) . f ( ) ) \ Color static method call {red} may be based on (e.g., of Cf ()) may be performed (e.g., C (). F ()) in the example.

Static methods in Python and Java or C ++ static methods are similar. See classmethod (), is used to create variants alternate class constructor.

Like all the decorators like, you can also call the same function as a conventional staticmethod, and perform some action on its results. Such functions need to reference the class from the body and in some cases you want to avoid automatic conversion to example method. For these cases, use this syntax:

class C:
    builtin_open = staticmethod(open)

For more information on static methods, please refer to the standard type hierarchy.

3 difference

  • Different configurations: class class method requires as their first argument CLS, the static method does not receive an implicit first parameter, its own instance of the example of the method as the first argument self;
  • Examples of methods need to call after the creation of instances, while the other two can be called directly depend on the class, after class still create instance methods and static methods can be used;
  • Java class constructor can be overloaded, python class method in the case of defining a plurality of analog java constructor;
  • Static method uses python and java as designed classes and instances can be invoked after the instance is created, without further create a new static method, using the parent class;
  • Static method Scenario: If you do not access any instance methods and properties in the method, purely by passing a parameter and returns a functional method of the data, then it is suitable for static methods are defined, it saves the overhead of object instantiation costs, this method is often on the outside of the class as a function of the presence of module layers is no problem, but on the category, only the type of service.
  • Method class usage scenarios: 1, as a factory method to create an object instance, for example, have built-in module datetime.date class class method widely used as a factory method, in order to create a date object. 2, if you want to call a static method inside the class, then the method defined as a class method is suitable, because if defined as static methods, then you have to explicitly reference the class A, this is not a good thing for inheritance .
# coding:utf-8
class Foo(object):
    """类三种方法语法形式"""
    x,y = 10,14
    def instance_method(self):
        pass
    @staticmethod
    def static_method_averag(*mixes):
        return sum(mixes) / len(mixes)

    @classmethod
    def class_method(cls):
        return cls.static_method_averag(cls.x,cls.y)

foo = Foo()
# 调用关系
# foo.instance_method()
# foo.static_method_averag(1,2,3)
# foo.class_method()
# print('----------------')
# Foo.static_method_averag(1,2,3)
# Foo.class_method()
# print('----------------')

# 内存关系
print(Foo.static_method_averag is  foo.static_method_averag)  #静态方法不会动
print(Foo.class_method is  foo.class_method)
print(Foo.instance_method is  foo.instance_method)  #实例化后 实例绑定这个方法



Reference:
https://zhuanlan.zhihu.com/p/28010894
https://docs.python.org/zh-cn/3/index.html

Published 105 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/x1131230123/article/details/104475923