设为首页 加入收藏

TOP

Python各式装饰器(二)
2015-07-16 12:57:30 来源: 作者: 【 】 浏览:21
Tags:Python 各式 装饰
cls):


? ? ? ? def __test(*args,**kw):


? ? ? ? ? ? clsName=re.findall('(\w+)',repr(cls))[-1]


? ? ? ? ? ? print 'Call %s.__init().'%clsName


? ? ? ? ? ? obj=cls(*args,**kw)


? ? ? ? ? ? if printValue:


? ? ? ? ? ? ? ? print 'value = %r'%obj.value


? ? ? ? ? ? return obj


? ? ? ? return __test


? ? return _test


?


>>> @test()


class sy(object):


? ? def __init__(self,value):


? ? ? ? self.value=value


?


>>> s=sy('hello world')


Call sy.__init().


value = 'hello world'


>>> @test(False)


class sy(object):


? ? def __init__(self,value):


? ? ? ? self.value=value


>>> s=sy('hello world')


Call sy.__init().


>>>?



?二、类式装饰器:装饰器本身是一个类,借用__init__()和__call__()来实现职能


1.装饰函数:被装饰对象是一个函数


[1]装饰器无参数:


a.被装饰对象无参数:


>>> class test(object):


? ? def __init__(self,func):


? ? ? ? self._func=func


? ? def __call__(self):


? ? ? ? return self._func()


?


? ?


>>> @test


def say():


? ? return 'hello world'


?


>>> say()


'hello world'


>>>?



b.被装饰对象有参数:


>>> class test(object):


? ? def __init__(self,func):


? ? ? ? self._func=func


? ? def __call__(self,*args,**kw):


? ? ? ? return self._func(*args,**kw)


?


? ?


>>> @test


def left(Str,Len):


? ? #The parameters of __call__ can be '(self,Str,Len)' in this case.


? ? return Str[:Len]


>>> left('hello world',5)


'hello'


>>>?



?[2]装饰器有参数


a.被装饰对象无参数:


>>> class test(object):


? ? def __init__(self,beforeinfo='Call function'):


? ? ? ? self.beforeInfo=beforeinfo


? ? def __call__(self,func):


? ? ? ? def _call():


? ? ? ? ? ? print self.beforeInfo


? ? ? ? ? ? return func()


? ? ? ? return _call


?


? ?


>>> @test()


def say():


? ? return 'hello world'


?


>>> say()


Call function


'hello world'


>>>?


或者:


>>> class test(object):


? ? def __init__(self,beforeinfo='Call function'):


? ? ? ? self.beforeInfo=beforeinfo


? ? def __call__(self,func):


? ? ? ? self._func=func


? ? ? ? return self._call


? ? def _call(self):


? ? ? ? print self.beforeInfo


? ? ? ? return self._func()


?


? ?


>>> @test()


def say():


? ? return 'hello world'


?


>>> say()


Call function


'hello world'


>>>?



?b.被装饰对象有参数:


>>> class test(object):


? ? def __init__(self,beforeinfo='Call function'):


? ? ? ? self.beforeInfo=beforeinfo


? ? def __call__(self,func):


? ? ? ? def _call(*args,**kw):


? ? ? ? ? ? print self.beforeInfo


? ? ? ? ? ? return func(*args,**kw)


? ? ? ? return _call


>>> @test()


def left(Str,Len):


? ? #The parameters of _call can be '(Str,Len)' in this case.


? ? return Str[:Len]


?


>>> left('hello world',5)


Call function


'hello'


>>>?


或者:


>>> class test(object):


? ? def __init__(self,beforeinfo='Call function'):


? ? ? ? self.beforeInfo=beforeinfo


? ? def __call__(self,func):


? ? ? ? self._func=func


? ? ? ? return self._call


? ? def _call(self,*args,**kw):


? ? ? ? print self.beforeInfo


? ? ? ? return self._func(*args,**kw)


?


? ?


>>> @test()


def left(Str,Len):


? ? #The parameters of _call can be '(self,Str,Len)' in this case.


? ? return Str[:Len]


?


>>> left('hello world',5)


Call function


'hello'


>>>?



?2.装饰类:被装饰对象是一个类


[1]装饰器无参数:


a.被装饰对象无参数:


>>> class test(object):


? ? def __init__(self,cls):


? ? ? ? self._cls=cls


? ? def __call__(self):


? ? ? ? return self._cls()


?


? ?


>>> @test


class sy(object):


? ? def __init__(self):


? ? ? ? self.value=32


?


? ?


>>> s=sy()


>>> s


<__main__.sy object at 0x0000000003AAFA20>


>>> s.value


32


>>>?



?b.被装饰对象有参数:


>>> class test(object):


? ? def __init__(self,cls):


? ? ? ? self._cls=cls


? ? def __call__(self,*args,**kw):


? ? ? ? return self._cls(*args,**kw)


?


? ?


>>> @test


class sy(object):


? ? def __init__(self,value):


? ? ? ? #The parameters of __call__ can be '(self,value)' in this case.


? ? ? ? self.value=value


?


? ? ? ?


>>> s=sy('hello world')


>>> s


<__main__.sy object at 0x0000000003AAFA20>


>>> s.value


'hello world'


>>>?



?[2]装饰器有参数:


a.被装饰对象无参数:


>>> class test(object):


? ? def __init__(

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉树的递归遍历和非递归(循环).. 下一篇Python IDLE 清屏问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: