1. 被装饰对象为函数,函数作为装饰器
1.1 被装饰函数无参数,装饰器函数无参数
def decrator(func): def wrapper(): print "decrator with no params testing" return func() return wrapper @decrator def test(): print "this is function with no params" test()
Result:
decrator with no params testing
this is function with no params
[Finished in 0.1s]
1.2 被装饰函数有参数,装饰器函数无参数
def decrator(func): def wrapper(*args,**kw): print "decrator with no params testing" return func(*args,**kw) return wrapper @decrator def test(message): print "this is function with no params" print "message is",message test('testing params')
Result:
decrator with no params testing
this is function with no params
message is testing params
[Finished in 0.1s]
1.3 被装饰函数无参数,装饰器函数有参数
def decrator(message): def _decrator(func): def wrapper(): print "decrator with no params testing" print message return func() return wrapper return _decrator @decrator('decrator params') def test(): print "this is function with no params" test()
Result:
decrator with no params testing
decrator params
this is function with no params
[Finished in 0.1s]
1.4 被装饰函数有参数,装饰器函数有参数
def decrator(message): def _decrator(func): def wrapper(*args,**kw): print "decrator with no params testing" print "decrator message is",message return func(*args,**kw) return wrapper return _decrator @decrator('decrator params') def test(message): print "this is function with no params" print "function message is",message test('function message')
Result:
decrator with no params testing
decrator message is decrator params
this is function with no params
function message is function message
[Finished in 0.1s]
2. 被装饰对象为函数,类作为装饰器
2.1 被装饰函数无参数,装饰器类无参数
class decrator(object): def __init__(self,cls): self.cls=cls def __call__(self): print "decrator class testing" return self.cls() @decrator def test(): print "this is function with no params" test()
Result:
decrator class testing
this is function with no params
[Finished in 0.1s]
2.2 被装饰函数有参数,装饰器类无参数
class decrator(object): def __init__(self,cls): self.cls=cls def __call__(self,*args,**kw): print "decrator class testing" return self.cls(*args,**kw) @decrator def test(message): print "this is function with no params" print "function messages is",message test('function params')
Result:
decrator class testing
this is function with no params
function messages is function params
[Finished in 0.1s]
2.3 被装饰函数无参数,装饰器类有参数
class decrator(object): def __init__(self,message): self.message=message def __call__(self,func): def _call(): print "decrator class testing" print self.message return func() return _call @decrator('decrator params') def test(): print "this is function with no params" test()
Result:
decrator class testing
decrator params
this is function with no params
[Finished in 0.1s]
2.4 被装饰函数有参数,装饰器类有参数
class decrator(object): def __init__(self,message): self.message=message def __call__(self,func): def _call(*args,**kw): print "decrator class testing" print self.message return func(*args,**kw) return _call @decrator('decrator params') def test(message): print "this is function with no params" print "function message is",message test('function params')
Result:
decrator class testing
decrator params
this is function with no params
function message is function params
[Finished in 0.1s]
3. 被装饰对象为类,函数作为装饰器
3.1 被装饰类无参数,装饰器函数无参数
def decrator(cls): def wrapper(): print "decrator function testing" obj = cls() return obj return wrapper @decrator class test(object): def __init__(self): print "here is test class init with no params" def run(self): print "function run" t=test()
Result:
decrator function testing
here is test class init with no params
[Finished in 0.1s]
3.2 被装饰类有参数,装饰器函数无参数
def decrator(cls): def wrapper(*args,**kw): print "decrator function testing" obj = cls(*args,**kw) return obj return wrapper @decrator class test(object): def __init__(self,message): self.message = message print "here is test class init with params" print self.message def run(self): print "function run" t=test('class params')
Result:
decrator function testing
here is test class init with params
class params
[Finished in 0.1s]
3.3 被装饰类无参数,装饰器函数有参数
def decrator(message): def _decrator(cls): def wrapper(): print "decrator function testing" obj = cls() return obj return wrapper return _decrator @decrator('decrator params') class test(object): def __init__(self): print "here is test class init with params" def run(self): print "function run" t=test()
Result:
decrator function testing
here is test class init with params
[Finished in 0.1s]
3.4 被装饰类有参数,装饰器函数有参数
def decrator(message): def _decrator(cls): def wrapper(*args,**kw): print "decrator function testing" obj = cls(*args,**kw) return obj return wrapper return _decrator @decrator('decrator params') class test(object): def __init__(self,info): self.info=info print "here is test class init with params" print self.info def run(self): print "function run" t=test('class params')
Reslut:
decrator function testing
here is test class init with params
class params
[Finished in 0.1s]
4. 被装饰对象为类,类作为装饰器
4.1. 被装饰类无参数,装饰器类无参数
class decrator(object): def __init__(self,cls): self.cls=cls def __call__(self): print "here is decrator class testing" return self.cls() @decrator class test(object): def __init__(self): print "here is test class init with no params" def run(self): print "function run" t=test()
Result:
here is decrator class testing
here is test class init with no params
[Finished in 0.1s]
4.2 被装饰类有参数,装饰器类无参数
class decrator(object): def __init__(self,cls): self.cls=cls def __call__(self,*args,**kw): print "here is decrator class testing" return self.cls(*args,**kw) @decrator class test(object): def __init__(self,message): self.message = message print "here is test class init with no params" print "class message is",self.message def run(self): print "function run" t=test('class params')
Result:
here is decrator class testing
here is test class init with no params
class message is class params
[Finished in 0.1s]
4.3 被装饰类无参数,装饰器类有参数
class decrator(object): def __init__(self,message): self.message=message def __call__(self,cls): def _call(): print "decrator class testing" print self.message self.cls=cls return cls() return _call @decrator('decrator params') class test(object): def __init__(self): print "here is test class init with no params" def run(self): print "function run" t=test()
Result:
decrator class testing
decrator params
here is test class init with no params
[Finished in 0.1s]
4.4 被装饰类有参数,装饰器类有参数
class decrator(object): def __init__(self,message): self.message=message def __call__(self,cls): def _call(*args,**kw): print "decrator class testing" print self.message self.cls=cls return cls(*args,**kw) return _call @decrator('decrator params') class test(object): def __init__(self,info): self.info=info print "here is test class init with no params" print info def run(self): print "function run" t=test('function params')
Result:
decrator class testing
decrator params
here is test class init with no params
function params
[Finished in 0.1s]
5. 装饰器参数传递给被装饰对象,作为被装饰对象参数使用
def decrator(message): def _decrator(cls): def wrapper(*args,**kw): print "decrator function testing" status = 0 args=tuple(list(args)+[status]) #1.将装饰器的参数加入到被装饰对象的参数列表中 obj = cls(*args,**kw) return obj return wrapper return _decrator @decrator('decrator params') class test(object): def __init__(self,info,status): #2.被装饰类定义参数 self.info=info self.status=status print "here is test class init with params" def run(self): print "function run" print self.info print self.status t=test('class params') #被装饰类初始化时候少一个参数,因为有一个参数是通过decrator传递了 t.run()
Result:
decrator function testing
here is test class init with params
function run
class params
0
[Finished in 0.2s]