詳解python三大器——迭代器、生成器、裝飾器
def decorator_get_function_duration(enable):
"""
:param enable: 是否需要統計函式執行耗時
:return:
"""
print("this is decorator_get_function_duration")
def inner(func):
print('this is inner in decorator_get_function_duration')
@wraps(func)
def wrapper(*args, **kwargs):
print('this is a wrapper in decorator_get_function_duration.inner')
if enable:
start = time.time()
print(f" 函式執行前: {start}")
result = func(*args, **kwargs)
print('[%s]`s enable was %s it`s duration : %.3f s ' % (func.__name__, enable, time.time() - start))
else:
result = func(*args, **kwargs)
return result
return wrapper
return inner
def decorator_1(func):
print('this is decorator_1')
@wraps(func)
def wrapper(*args, **kwargs):
print('this is a wrapper in decorator_1')
return func(*args, **kwargs)
return wrapper
def decorator_2(func):
print('this is decorator_2')
@wraps(func)
def wrapper(*args, **kwargs):
print('this is a wrapper in decorator_2')
return func(*args, **kwargs)
return wrapper
@decorator_1 # 外匯跟單gendan5.com 此處相當 :decorator_1(decorator_2(decorator_get_function_duration(enable=True)(fun)))
@decorator_2 # = decorator_2(decorator_get_function_duration(enable=True)(fun))
@decorator_get_function_duration(enable=True) # = decorator_get_function_duration(enable=True)(fun)
def fun():
time.sleep(2)
print("fun 執行完了~ ")
fun()
# ======== enable=False ============
"""
this is decorator_get_function_duration
this is inner in decorator_get_function_duration
this is decorator_2
this is decorator_1
this is a wrapper in decorator_1
this is a wrapper in decorator_2
this is a wrapper in decorator_get_function_duration.inner
fun 執行完了~
"""
# ======== enable=True ============
"""
this is decorator_get_function_duration
this is inner in decorator_get_function_duration
this is decorator_2
this is decorator_1
this is a wrapper in decorator_1
this is a wrapper in decorator_2
this is a wrapper in decorator_get_function_duration.inner
函式執行前: 1634635708.648994
fun 執行完了~
[fun]`s enable was True it`s duration : 2.002 s
"""
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2838407/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python迭代器&生成器&裝飾器Python
- python_裝飾器——迭代器——生成器Python
- Python之裝飾器、迭代器和生成器Python
- Python學習之三大名器-裝飾器、迭代器、生成器Python
- python基礎(補充):python三大器之裝飾器Python
- Python裝飾器詳解Python
- python基礎(補充):python三大器之生成器Python
- Python中裝飾器語法詳解Python
- Python裝飾器例項講解(三)Python
- python 生成器&迭代器Python
- Python迭代器與生成器Python
- Python之可迭代物件、迭代器、生成器Python物件
- python裝飾器2:類裝飾器Python
- Python語法—迭代器、生成器Python
- 1.5.4 Python迭代器和生成器Python
- c#裝飾器模式詳解C#模式
- Python 裝飾器Python
- Python裝飾器Python
- Python系列(三):關於迭代器和生成器,你該瞭解這些Python
- 史上最全 Python 迭代器與生成器Python
- python3.7 迭代器和生成器Python
- python3 裝飾器全解Python
- 瞭解python的裝飾器特性Python
- 迭代器、生成器
- 迭代器,生成器
- 閉包函式,裝飾器詳解函式
- Python3中的列表生成式、生成器與迭代器例項詳解Python
- 1.5.3 Python裝飾器Python
- Python 裝飾器(一)Python
- python的裝飾器Python
- Python 裝飾器原理Python
- Python裝飾器模式Python模式
- 可迭代物件、迭代器、生成器物件
- 搞清楚 Python 的迭代器、可迭代物件、生成器Python物件
- 草根學Python(七) 迭代器和生成器Python
- Python基礎(四)——迭代器/物件,生成器Python物件
- python迭代器和生成器的總結Python
- 迭代器與生成器