day20 Python 高阶函数,函数,嵌套,闭包 装饰器

高阶函数定义

1.函数接收的参数是一个函数名

2.函数的返回值是一个函数名

3.满足上述条件任意一个都可以称之为高阶函数

一、函数的接收参数是一个函数名

import time
def foo():
    time.sleep(3)
    print('name is charon')

def test(func):
    #print(func)
    start_time=time.time()
    func()
    stop_time=time.time()
    print('run time %s' %(stop_time-start_time))
#foo()
test(foo)

结果:
name is charon
run time 3.0033559799194336

 二、函数的返回参数是一个函数名,实现不了这个功能

import time
def foo():
    time.sleep(3)
    print('来自foo')

#不修改foo源代码
#不修改foo调用方式


#多运行了一次,不合格
# def timer(func):
#     start_time=time.time()
#     func()
#     stop_time = time.time()
#     print('函数运行时间是  %s' % (stop_time-start_time))
#     return func
# foo=timer(foo)
# foo()
将timmer函数赋值给foo函数,上面return给timmer函数


#没有修改被修饰函数的源代码,也没有修改被修饰函数的调用方式,但是也没有为被修饰函数添加新功能
def timer(func):
    start_time=time.time()
    return func
    stop_time = time.time()
    print('函数运行时间是  %s' % (stop_time-start_time))

foo=timer(foo)
foo()

 函数嵌套定义

在def里面又定义了一个def,函数里面定义了一个函数

def father(name):
    print('from father %s' %name)
    def son():
        name='linhaifeng_1'
        print('我的爸爸是%s' %name)
        def grandson():
            name='linhaifeng_2'
            print('我的爷爷是%s' %name)
        grandson()
    # print(locals())
    son()
# father('linhaifeng')
father('linhaifeng')


如果:
我的爸爸是linhaifeng_1
我的爷爷是linhaifeng_2

闭包定义

一层一层的

def father(auth_type):
    # print('from father %s' %name)
    def son():
        # name='linhaifeng_1'
        # print('我的爸爸是%s' %name)
        def grandson():
            print('我的爷爷是%s' %auth_type)
        grandson()
    # print(locals())
    son()
# father('linhaifeng')
father('filedb')

 装饰器=高阶函数+函数嵌套+闭包

1.不修改原函数的源代码

2.不修改函数的调用方式

import time
def timmer(func):#func=test
    def wrapper():
        start_time=time.time()
        #print(func)
        func()#就是在运行test()
        stop_time=time.time()
        print('运行时间是%s' %(stop_time-start_time))
    return wrapper
@timmer #test=timmer(test)
def test():
    time.sleep(3)
    print('test函数运行完毕')

#test=timmer(test)
test()
# res=timmer(test)  #返回的是wrapper的地址
# res()  #执行的是wrapper()

# test=timmer(test)  #返回的是wrapper的地址
# test()  #执行的是wrapper()

#  @timmer  就相当于 test=timmer(test)

 加上返回值

import time
def timmer(func): #func=test
    def wrapper():
        start_time=time.time()
        res=func() #就是在运行test()
        stop_time = time.time()
        print('运行时间是%s' %(stop_time-start_time))
        return res
    return wrapper

@timmer #test=timmer(test)
def test():
    time.sleep(3)
    print('test函数运行完毕')
    return '这是test的返回值'

res=test()  #就是在运行wrapper
print(res)

结果:
test函数运行完毕
运行时间是3.001859188079834
这是test的返回值

 加上参数

import time
def timmer(func): #func=test1
    def wrapper(*args,**kwargs): #test('linhaifeng',age=18)  args=('linhaifeng')  kwargs={'age':18}  *args,**kwargs:接收任意可变长参数,
        start_time=time.time()
        res=func(*args,**kwargs) #就是在运行test()         func(*('linhaifeng'),**{'age':18})
        stop_time = time.time()
        print('运行时间是%s' %(stop_time-start_time))
        return res
    return wrapper

@timmer #test=timmer(test)
def test(name,age):
    time.sleep(3)
    print('test函数运行完毕,名字是【%s】 年龄是【%s】' %(name,age))
    return '这是test的返回值'

@timmer
def test1(name,age,gender):
    time.sleep(1)
    print('test1函数运行完毕,名字是【%s】 年龄是【%s】 性别【%s】' %(name,age,gender))
    return '这是test的返回值'

# res=test('linhaifeng',age=18)  #就是在运行wrapper
# # print(res)
# test1('alex',18,'male')
test('charon',19)
test1('alex',18,'male')

结果:
test函数运行完毕,名字是【charon】 年龄是【19】
运行时间是3.003596305847168
test1函数运行完毕,名字是【alex】 年龄是【18】 性别【male】
运行时间是1.0006954669952393





加上*,**让怎么来怎么出去

# def test2(name,age,gender): #test2(*('alex',18,'male','x','y'),**{})
#     #name,age,gender=('alex',18,'male','x','y')
#     print(name)
#     print(age)
#     print(gender)
#
# def test1(*args,**kwargs):
#     test2(*args,**kwargs)  #args=('alex',18,'male','x','y') kwargs={}
#
# # test2('alex',18,gender='male')
#
# test1('alex',18,'male')

 闭包补充解压序列

l = [10,3,2,3,3,5,31,4]
a,b,c,d,e,f,g,h=l
print(a)
a,*_,c=l
print(a)
print(c)
a,b,*_,c,d = l
print(a,b,c,d)

结果:
10
10
4
10 3 31 4

 换值

a=1
b=2
x=a
a=b
b=x
print(a,b)

结果:
2 1

f1 = 1
f2 = 2
f1,f2 = f2,f1
print(f1,f2)

结果:
2 1X

猜你喜欢

转载自www.cnblogs.com/charon2/p/10391148.html