Python装饰器
发布时间:2020-12-20 10:22:16 所属栏目: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 = classmet
# 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_methodfoothis is a some_class_methodfoo (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |