网站制作学习网Python→正文:python 将类方法转换为静态属性方法
字体:

python 将类方法转换为静态属性方法

Python 2024/6/26 8:58:09  点击:不统计

原文是网站制作学习网的FoAsP.cn
 python 中,可以将python中的方法转换为静态属性方法,可以通过类名称直接调用而不用实例化对象。

代码如下:
 
 
class A:
    
    def test():
        return "forasp.cn"
    
    name = staticmethod(test)
 
 
print(A.name())
 
 
上述代码中的 def test 方法,没有self 或者cls 参数。
通过 staticmethod 将test 方法转变为静态方法,并给A类的属性name
通过A.name() 直接调用静态化后的方法。

原文章%77w%77%2Ef%6F%72%61%73%70%2E%63n

·上一篇:python 多重继承 super的运行顺序 >>    ·下一篇:python抛出异常 >>
推荐文章
最新文章