本文主要是介绍python decorator 之函数装饰类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python decorator 函数装饰类
函数装饰类,在每次类实例化对象之前调用:
def wrapClass(cls):def inner(a):print('class name:', cls.__name__)return cls(a)return inner@wrapClass
class Foo():def __init__(self, a):self.a = adef fun(self):print('fun self.a =', self.a)def fun2(self):print('fun2 self.a = ', self.a)obj1 = Foo('object 1')
obj1.fun()
obj1.fun2()print()
obj2 = Foo('object 2')
obj2.fun()
obj2.fun2()
output:
class name: Foo
fun self.a = object 1
fun2 self.a = object 1class name: Foo
fun self.a = object 2
fun2 self.a = object 2
这篇关于python decorator 之函数装饰类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!