zoukankan      html  css  js  c++  java
  • classmethod staticmethod

    decorator

    Glossary — Python 3.6.3 documentation https://docs.python.org/3/glossary.html

    decorator

    A function returning another function, usually applied as a function transformation using the @wrapper syntax. Common examples for decorators are classmethod() and staticmethod().

    The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:

    公司的员工是一个类,每实例化一个员工类,公司人数+1,Python中是通过classmethod 实现 

    追问 如果 员工数目 达到123 就禁止实例化员工类 说说 实现思路

    @classmethod

    Transform a method into a class method.

    A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

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

    The @classmethod form is a function decorator – see the description of function definitions in Function definitions for details.

    It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

    Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in this section.

    For more information on class methods, consult the documentation on the standard type hierarchy in The standard type hierarchy.

    @staticmethod

    Transform a method into a static method.

    A static method does not receive an implicit first argument. To declare a static method, use this idiom:

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

    The @staticmethod form is a function decorator – see the description of function definitions in Function definitions for details.

    It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

    Static methods in Python are similar to those found in Java or C++. Also see classmethod() for a variant that is useful for creating alternate class constructors.

    Like all decorators, it is also possible to call staticmethod as a regular function and do something with its result. This is needed in some cases where you need a reference to a function from a class body and you want to avoid the automatic transformation to instance method. For these cases, use this idiom:

    class C:
    builtin_open = staticmethod(open)

    For more information on static methods, consult the documentation on the standard type hierarchy in The standard type hierarchy.

    Difference between @staticmethod and @classmethod in Python - Python Central http://www.pythoncentral.io/difference-between-staticmethod-and-classmethod-in-python/

  • 相关阅读:
    docker 目录移动到其他磁盘的操作
    linux -- 查看磁盘空间的大小 查看文件夹占用磁盘空间大小
    python flask 反向代理
    shell 脚本换行符的问题
    uniapp使用web-view跳转vue单页面通信,互发消息
    java基础(多线程---lambda)
    SpringBoot开发详解(五)--Controller接收参数以及参数校验
    使用 TypeScript 来开发 React 的注意事项
    video标签通过js实现增加倍速播放功能
    zblog调取置顶文章
  • 原文地址:https://www.cnblogs.com/rsapaper/p/7894517.html
Copyright © 2011-2022 走看看