Decorator (decorator)
- Function basis
# Function.py function # 1) is the object function DEF Hello (): return ' ! The Hello, World ' FUNC = Hello Print (FUNC) # <Object> Print (FUNC ()) # 2) can be nested functions, defined in another function of the internal DEF Show (): Print ( ' the Run in Show () ' ) DEF Message (= Word ' the Hello ' ): return Word Print (Message ()) Show () # . 3) function returns as an argument DEF getWordType ( kind =''): def default(word=''): return word def upper(word=''): return word.upper() def lower(word=''): return word.lower() if kind.lower() == 'lower': return lower elif kind.lower() == 'upper': return upper else: return default wordType = getWordType('upper') print(wordType) print(wordType('Resistance is futile!')) # 4)函数作为参数传入 def getName(name='leo'): return name def foo(func): print('I will call the getName function later') print(func()) foo(getName)
- Decorator and a function to compare
# decorator.py 装饰器 # 不使用装饰器情况 def my_new_decorator(a_function_to_decorator): def the_wrapper_around_the_original_function(): print('Befor the function runs') a_function_to_decorator() print('After the function runs') return the_wrapper_around_the_original_function # 将函数作为参数返回 def a_stand_alone_function(): print("I'm a stand alone function, don't you dare modify me.") a_stand_alone_function() a_stand_alone_function_decorated = my_new_decorator(a_stand_alone_function) # 将函数作为参数传入 print(a_stand_alone_function_decorated) a_stand_alone_function_decorated() # 使用装饰器 语法糖"@" @my_new_decorator def another_stand_alone_function(): print('leave me alone') another_stand_alone_function() # same as: another_stand_alone_function = my_new_decorator(another_stand_alone_function)
- Decorative applications
# decorator_web.py web中的装饰器应用 def makebold(func): def wrapper(): return '<b>' + func() + '</b>' return wrapper def makeitalic(func): def wrapper(): return '<i>' + func() + '</i>' return wrapper @makebold @makeitalic def word(): return 'Hello, decorator!' print(word())
# Decorator_time.py decorator check function of operating time Import Time Import OS DEF Timer (FUNC): DEF warpper (CWD): time_begin = time.ctime () Print ( ' the Begin @ ' + time_begin) FUNC (CWD) TIME_END = time.ctime () Print ( ' End @ ' + TIME_END) Print ( ' the Begin @ ' + time_begin) Print ( ' End @ '+ TIME_END) return warpper @timer DEF Walk (R & lt CWD = ' D: \ PyCharm \ Python \ Practice ' ): for the root, dirs, Files in os.walk (CWD): # Print (the root) directory of Print # all subdirectories absolute path # Print (dirs) # print the list of subdirectories in each directory # Print (files) # print a list of all files under Print (root) for file in files: Print ( ' \ t ' + file ) Walk (CWD = R & lt ' D: \ PyCharm ' )
- It contains all the parameters and the return value of the decorator
# decorator_args.py 包含所有参数的装饰器 import time def timer(func): def inner(*args, **kwargs): start = time.time() re = func(*args, **kwargs) end = time.time() return re return inner @timer #==> func1 = timer(func1) def func1(a, b): print('in func1') return a, b @timer #==> func2 = timer(func2) def func2(a): print('In func2 and get', a) return 'func2 over' func1('aaaaaa', 'bbbbbb') print(func2('aaaaaa'))
- That the function is a normal function can be used decorated method
# decorator_method.py from functools import wraps def deco(func): @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @deco def index(): '''docs here in decorator''' print('from index') index() print(index.__name__) print(index.__doc__)