Python 3 教程 Python staticmethod() 函数

tineke · 2022-03-16 23:24:39 · 热度: 13

python staticmethod 返回函数的静态方法。

该方法不强制要求传递参数,如下声明一个静态方法:

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

以上实例声明了静态方法 f,从而可以实现实例化使用 C().f(),当然也可以不实例化调用该方法 C.f()

函数语法

staticmethod(function)

参数说明:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- class C(object): @staticmethod def f(): print('codercto'); C.f(); # 静态方法无需实例化 cobj = C() cobj.f() # 也可以实例化后调用

以上实例输出结果为:

codercto
codercto

查看更多 Python 内置函数

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册