decorator make a wrapper function do something before and after the original function. The wrapper function share arguments with original function.@decorator is same with func = decorator(func); decorator receive func and return a wrapper func with same arguments with original func. The decorator function is done in @decorator progress. Wrapper fucntion in decorator share the same argument with original function and run the original function in itself. If you want decotrator receive some arguements(as decorator receiv function arguemnt return arguments funcion), you need the decotrator return a maker function which receive argument and return wrapper function(same arguments with original function). The sample is as follows.
def mydecorator_maker(*args, **kwargs):
print "this is decorator maker function..."
print args, kwargs
def mydecorator(func):
print "this is decorator... args is passed here"
print args, kwargs
def decorator_wrapper(func_arg1, func_arg2):
print "this is decorator wrapper, before function..."
print "func_arg1 " + func_arg1 + " func_arg2 " + func_arg2
func(func_arg1, func_arg2)
print "this is decorator wrapper, after function"
return decorator_wrapper
return mydecorator
@mydecorator_maker(1, 2, 3, test="test")
def mydecorated_function(func_arg1, func_arg2):
print "this is decorated function with arg1 " + func_arg1 + " arg2 " + func_arg2
This is another example for decorated doecorator. It works also with maker and with interation. Nothing special from above maker function
def decorator_with_args(decorator_to_enhance):
"""
This function is supposed to be used as a decorator.
It must decorate an other function, that is intended to be used as a decorator.
Take a cup of coffee.
It will allow any decorator to accept an arbitrary number of arguments,
saving you the headache to remember how to do that every time.
"""
# We use the same trick we did to pass arguments
def decorator_maker(*args, **kwargs):
# We create on the fly a decorator that accepts only a function
# but keeps the passed arguments from the maker.
def decorator_wrapper(func):
# We return the result of the original decorator, which, after all,
# IS JUST AN ORDINARY FUNCTION (which returns a function).
# Only pitfall: the decorator must have this specific signature or it won't work:
return decorator_to_enhance(func, *args, **kwargs)
return decorator_wrapper
return decorator_maker
# You create the function you will use as a decorator. And stick a decorator on it :-)
# Don't forget, the signature is "decorator(func, *args, **kwargs)"
@decorator_with_args
def decorated_decorator(func, *args, **kwargs):
def wrapper(function_arg1, function_arg2):
print "Decorated with", args, kwargs
return func(function_arg1, function_arg2)
return wrapper
# Then you decorate the functions you wish with your brand new decorated decorator.
@decorated_decorator(42, 404, 1024)
def decorated_function(function_arg1, function_arg2):
print "Hello", function_arg1, function_arg2
decorated_function("Universe and", "everything")
#outputs:
#Decorated with (42, 404, 1024) {}
#Hello Universe and everything
# Whoooot!
decorated_decorator = decorator_with_args(decorated_decorator)
I will use a decorated docorator to receive arguments instend of maker functions. I think this is a really realization of intetraion.
def decorator_enhance_decorator(decorator_to_enhancement):
print "This decorated decorator will enhance decorator function..."
print decorator_to_enhancement
#def decorator_enhancer(*args, **kwargs):
def decorator_enhancer(*args, **kwargs):
print "decorator enhancer do something with args..."
print args
print kwargs
return decorator_to_enhancement
return decorator_enhancer
#return decorator_enhance_maker
@decorator_enhance_decorator
def decorated_decorator3(func):
print func
print "func is decorated here"
def wrapper(func_arg1, func_arg2):
print "wrapper with arg1 " + func_arg1 + " and arg2 " + func_arg2
return func(func_arg1, func_arg2)
return wrapper
@decorated_decorator3(1, 2, 3, test="test")
def decorated_function3(func_arg1, func_arg2):
print "decorated function with func arg1 " + func_arg1 + " arg2" + func_arg2
print "do something in decorated function"
decorated_function3("Liliy", "Baby")
mydecorated_function("liliy", "funny")