heading python decorator


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.

  • so a maker functions will return wrapper function with arguments same with original fucntion.
  • We user maker functions to receive arguments for wrapper functions. So wrapper functions can do something with some other arguments.
In [67]:
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 decorator maker function...
(1, 2, 3) {'test': 'test'}
this is decorator... args is passed here
(1, 2, 3) {'test': 'test'}
 

This is another example for decorated doecorator. It works also with maker and with interation. Nothing special from above maker function

In [1]:
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  
In [2]:
# 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 with (42, 404, 1024) {}
Hello Universe and everything
In [3]:
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.

  • Decorator means a function change from orginal function to wrapper function in decorator function.
  • The decorater function here will be change to a function do something with arguments and return the deocrator(which change original fucntion to wrapper).
  • We can make this change by a decorated decorator.
  • The decorated decorator receive decorator return a enhancer function dothing something with arguments. The function return the decorator at last. The enchancer function provide a enhacement to decorator with ablity to do something with some arguments.
In [71]:
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")
 
This decorated decorator will enhance decorator function...
<function decorated_decorator3 at 0x10b7755f0>
decorator enhancer do something with args...
(1, 2, 3)
{'test': 'test'}
<function decorated_function3 at 0x10b775668>
func is decorated here
wrapper with arg1 Liliy and arg2 Baby
decorated function with func arg1 Liliy arg2Baby
do something in decorated function
In [47]:
mydecorated_function("liliy", "funny")
 
this is decorator wrapper, before function...
func_arg1 liliy func_arg2 funny
this is decorated function with arg1 liliy arg2 funny
this is decorator wrapper, after function

你可能感兴趣的:(heading python decorator)