Decorator in python and python decorator
What is a decoration device?
Assume that all functions A, B, and C have been compiled. In this case, you find that A, B, and C all require the same function. What should you do?
A: decorator
The decorator is actually a function, but the return value of this function is a function.
In my personal understanding, the decorator is mainly used to complete the above function. function A, function B, and Function C are wrapped in function D, and function D is executed before or after function, handle some things
#! /Usr/bin/env python # coding: utf-8def SeparatorLine (): print "###########################" # decorator with parameter functions and parameter def decratorArgFuncArg (f1, f2): def inner (func): def wrapper (arg): print "decorator with Parameter Function with parameter" f1 () result = func (arg) f2 () return result return wrapper return inner # decorator with Parameter Function Without parameter def DecratorArgFuncNoArg (f1, f2): def inner (func): def wrapper (): print "decorator with Parameter Function Without parameter" f1 () result = func () f2 () return result return wrapper return inner # function without parameter decorator def FuncNoArgDecrator (func): def wrapper (): print "decorator without parameters in the function" func () return wrapper # decorator def FuncArgDecrator (func): def wrapper (arg ): print "decorator with parameters in the function" func (arg) return wrapper # decorator def FuncReturnDecrator (func): def wrapper (): print "decorator with returned values in the function" result = func () return result return wrapper # the two functions use def login (): print 'to start logging on to 'def logout (): print 'logout logins '@ FuncArgDecratordef Lee (arg): print' I am % s' % arg @ FuncNoArgDecratordef Marlon (): print 'I am Marlon' @ DecratorArgFuncNoArg (login, logout) def Allen (): print 'I am Allen' @ DecratorArgFuncArg (login, logout) def Aswill (name): print 'I am % s' % name @ FuncReturnDecratordef Frank (): return 'I am frank' if _ name __= =' _ main _ ': SeparatorLine () Lee ('Lee') SeparatorLine () Marlon () SeparatorLine () allen () SeparatorLine () Aswill ('aswill ') SeparatorLine () result = Frank () print result