@staticmethod或@classmethod

@staticmethod或@classmethod不需要实例化,

可直接调用函数,即不需要传参对象函数self和类函数cls

class A(object):
bar = 1
def foo(self):
print 'foo'

@staticmethod
def static_foo():
print 'static_foo'
print A.bar

@classmethod
def class_foo(cls):
print 'class_foo'
print cls.bar
cls().foo()
###执行
A.static_foo()
A.class_foo()

posted @ 2021-07-05 11:20  布都御魂  阅读(37)  评论(0编辑  收藏  举报