Python 嵌套函数和装饰器

1.嵌套函数:

def p_deco(func):
    def wrapper(name):
        return "<p>{}</p>".format(func(name))
    return wrapper
# @p_deco
def book(name):
    return 'the name of my book is {}'.format(name)

laoqi=p_deco(book)
py_book=laoqi("python 大学实用教程")

# py_book=book("python 大学实用教程")
print(py_book)

运行结果:

<p>the name of my book is python 大学实用教程</p>

2.装饰器:

def p_deco(func):
    def wrapper(name):
        return "<p>{}</p>".format(func(name))
    return wrapper
@p_deco
def book(name):
    return 'the name of my book is {}'.format(name)

# laoqi=p_deco(book)
# py_book=laoqi("python 大学实用教程")

py_book=book("python 大学实用教程")
print(py_book)

运行结果:

<p>the name of my book is python 大学实用教程</p>

3.装饰器:

'''
编写一个用于测试函数执行时间的装饰器函数
'''
import time
def timing_func(func):
    def wrapper():
        start=time.time()
        func()
        stop=time.time()
        return stop - start
    return wrapper

@timing_func
def test_list_append():
    lst=[]
    for i in range(1000000):
        lst.append(i)
@timing_func
def test_list_compare():
    return [i for i in range(1000000)]
    # print(x)

a = test_list_append()
b=test_list_compare()
print('time is:',a,b)

运行结果:

time is: 0.23202824592590332 0.10002374649047852

4.带参数的装饰器:

import time
from functools import wraps

def timethis(func):
    @wraps(func)
    def wrapper(*args,**kwargs):
        start=time.time()
        func(*args,**kwargs)
        end=time.time()
        print(func.__name__, end-start)
    return wrapper

@timethis
def countdown(n):
    while n>0:
        n-=1
@timethis
def test_list_append():
    lst=[]
    for i in range(1000000):
        lst.append(i)
@timethis
def test_list_compre():
    return [i for i in range(1000000)]

countdown(1000000)
countdown(100000000)
test_list_append()
test_list_compre()

运行结果:

countdown 0.08511972427368164
countdown 8.866152048110962
test_list_append 0.18751144409179688
test_list_compre 0.10936117172241211

示例1:

# -*- encoding:utf-8 -*-
from functools import wraps
 
def test(func):
    def wrapper(*args,**kwargs):
        '''我是说明1'''
        print('args: ',args)
        print('kwargs: ',kwargs)
        args = (11,22,33)
        kwargs['name'] = 'Test_C'
        return func(*args,**kwargs)
    return wrapper
 
 
@test
def fun_test(*args,**kwargs):
    '''我是说明2'''
    print('我是一个函数')
    print('---',args,kwargs)
 
 
fun_test(1,2,3,4,a=123,b=456,c=789)
print('*'*20)
print(fun_test.__name__)
print(fun_test.__doc__)

结果:

args:  (1, 2, 3, 4)
kwargs:  {
    
    'a': 123, 'b': 456, 'c': 789}
我是一个函数
--- (11, 22, 33) {
    
    'a': 123, 'b': 456, 'c': 789, 'name': 'Test_C'}
********************
wrapper
我是说明1

示例2:

# -*- encoding:utf-8 -*-
from functools import wraps
 
def test(func):
    @wraps(func)
    def wrapper(*args,**kwargs):
        '''我是说明1'''
        print('args: ',args)
        print('kwargs: ',kwargs)
        args = (11,22,33)
        kwargs['name'] = 'Test_C'
        return func(*args,**kwargs)
    return wrapper
 
 
@test
def fun_test(*args,**kwargs):
    '''我是说明2'''
    print('我是一个函数')
    print('---',args,kwargs)
 
 
fun_test(1,2,3,4,a=123,b=456,c=789)
print('*'*20)
print(fun_test.__name__)
print(fun_test.__doc__)

结果:

args:  (1, 2, 3, 4)
kwargs:  {
    
    'a': 123, 'b': 456, 'c': 789}
我是一个函数
--- (11, 22, 33) {
    
    'a': 123, 'b': 456, 'c': 789, 'name': 'Test_C'}
********************
fun_test
我是说明2

示例转自:https://blog.csdn.net/weixin_42544006/article/details/93506206

猜你喜欢

转载自blog.csdn.net/zhaoweiya/article/details/108891233