pythonのdecorator@
3529 ワード
decoratorはpythonの特性です.
decoratorは関数であってもクラスであってもよい.
decorator@の用途:
1)パラメータチェック,呼び出し統計など,関数の開始前に初期作業を行う.
2)関数の終了後にいくつかの終了作業を行う.
3)C++テンプレートのような機能.
decoratorが関数の場合、
実行結果:
hello() is called:
do something before fun!
args is less
do something after fun!
-----------------------------
hello(1) is called:
do something before fun!
hello
do something after fun!
chain0
chain1
chain2
具体的な原因は注釈を見れば分かります.
decoratorがクラスの場合、
実行結果:
__init__
__init__
-------------------------
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls =
hello
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls =
world
fooインスタンスを作成するとき、
fooの関数に対応するクラスdecoratorがあれば、
クラスdecoratorもそれぞれ対応する初期化を行います.
__get__その後、関数を返し、前の関数decoratorと同じになります.
decoratorは関数であってもクラスであってもよい.
decorator@の用途:
1)パラメータチェック,呼び出し統計など,関数の開始前に初期作業を行う.
2)関数の終了後にいくつかの終了作業を行う.
3)C++テンプレートのような機能.
decoratorが関数の場合、
def decorator(fun):
def call(*args, **kwargs):
print "do something before fun!"
if len(args) < 1:
print "args is less"
else:
fun(args[0])
print "do something after fun!"
return call
@decorator
def hello(a):
print "hello"
# hello(*args, **kwargs)
# is equivalent to
# decorator(hello)(*args, **kwargs)
# see the example for more detail
print "hello() is called:"
hello()
print "-----------------------------"
print "hello(1) is called:"
hello(1)
# decorator chain
def chain0(fun):
def call(*args0, **kwargs0):
print "chain0"
fun(*args0, **kwargs0)
return call
def chain1(fun):
def call(*args1, **kwargs1):
print "chain1"
fun(*args1, **kwargs1)
return call
@chain0
@chain1
def chain2(*args2, **kwargs2):
print "chain2"
# attention!!
# chain2(*args0, **kwargs0)
# is equivalent to chain0(chain1(chain2))(*args0, **kwargs0)
# it is not chain2(*args2, **kwargs2)
# it is not chain0(chain1(chain2(*args0, **kwargs0)))
# it is not chain0(chain1(chain2(*args2, **kwargs2)))
chain2()
実行結果:
hello() is called:
do something before fun!
args is less
do something after fun!
-----------------------------
hello(1) is called:
do something before fun!
hello
do something after fun!
chain0
chain1
chain2
具体的な原因は注釈を見れば分かります.
decoratorがクラスの場合、
class decorator(object):
def __init__(self, func):
print func
self.func = func
print '__init__'
def __call__(self, *args, **kwargs):
print 'obj = %s, cls = %s' % (self.obj, self.cls)
return self.func.__call__(*args, **kwargs)
def __get__(self, obj, cls):
self.obj = obj
self.cls = cls
print '__get__'
return self.__call__
class foo(object):
@decorator
def hello(self):
print 'hello'
@decorator
def world(self):
print 'world'
tmp = foo()
print '-------------------------'
tmp.hello(tmp)
tmp.world(tmp)
実行結果:
__init__
__init__
-------------------------
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls =
hello
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls =
world
fooインスタンスを作成するとき、
fooの関数に対応するクラスdecoratorがあれば、
クラスdecoratorもそれぞれ対応する初期化を行います.
__get__その後、関数を返し、前の関数decoratorと同じになります.