Just a function
def hoge():
print "aaaaaaabb"
hoge()
aaaaaaabb
There is nothing strange about it.
In fact, Python is @ (function name) You can create a nested structure by adding the description before the function definition. So if you play with it a little
def deco(func):
def aaa():
print "wei soiya"
func()
return aaa
@deco
def hoge():
print "aaaaaaabb"
hoge()
wei soiya
aaaaaaabb
A sentence will be added before
In the above example
@deco
def hoge
By
hoge()
But
deco(hoge)()
Is equivalent to.
It seems that you can combine multiple decorators, or you can pass arguments to the decorator separately by biting the wrapper.
def deco1(name):
def wrapper(func):
def aaaa():
print "your name is "+name
print "deco1 called!"
return func("aaa","www",yakiniku="teishoku")
return aaaa
return wrapper
def deco2(func):
def bbbb(*args, **kwargs):
print "wrap2 called!"
return func(*args, **kwargs)
return bbbb
@deco1('nanntara-kanntara-')
@deco2
def eee(*args, **kwargs):
print args
print kwargs
return "done"
print eee()
your name is nanntara-kanntara-
deco1 called!
wrap2 called!
('aaa', 'www')
{'yakiniku': 'teishoku'}
done
I referred to the following site.
http://hangar.runway7.net/python/decorators-and-wrappers
Recommended Posts