Python 内建函数 - staticmethod(function)

Manual

Return a static method for function.

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.

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

直译

function返回一个静态方法

静态方法不接收隐式首参数,可以用以下语句来定义一个静态方法:

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

@staticmethod是函数装饰器形式,函数定义的描述详见函数定义
该方法既可以在类上调用(如C.f()),也可以在实例上调用(如C().f())。

Python中的静态方法与Java或C++的相似,同样classmethod()对于创建代替的类构造器也很有用。

更多静态方法的信息,查看标准类型层级

拓展阅读

装饰器
函数定义
classmethod()
标准类型层级

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值