Python装饰器
Posted 千面佛缘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python装饰器相关的知识,希望对你有一定的参考价值。
# class WithoutDecorators: # def some_static_method(): # print(‘this is a static method‘) # some_static_method = staticmethod(some_static_method) # def some_class_method(cls): # print(‘this is a class method‘) # some_class_method = classmethod(some_class_method) # WithoutDecorators.some_class_method() # WithoutDecorators.some_static_method() #类的静态方法申明(装饰模式)) class WithoutDecorators: def foo(self): print(‘foo‘) @staticmethod def some_static_method(): print(‘this is a some_static_method‘) WithoutDecorators.foo(WithoutDecorators()) #静态方法在调用类的非静态方法时候需要实例化类本身 #因此静态方法申明尽量只处理单一的与类属性无关的逻辑 @classmethod def some_class_method(cls): print(‘this is a some_class_method‘) cls().foo() #静态类方法调用本身利用cls指针去调用类的非静态方法 WithoutDecorators.some_static_method() WithoutDecorators.some_class_method()
输出结果:
this is a some_static_method
foo
this is a some_class_method
foo
以上是关于Python装饰器的主要内容,如果未能解决你的问题,请参考以下文章