python 将类方法转换为静态属性方法
原文是网站制作学习网的FoAsP.cn
python 中,可以将python中的方法转换为静态属性方法,可以通过类名称直接调用而不用实例化对象。
原文章%77w%77%2Ef%6F%72%61%73%70%2E%63n
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