python 高阶函数

#高阶函数的定义:
#1.函数接收的参数是一个函数名
#2.函数的返回值是一个函数名
#3.满足上述条件的任意一个都可以称为高阶函数

import time
def foo(): ##执行函数foo,输出你好啊eddy
    print('你好啊eddy') 
def test(func): ###执行函数test()执行时需带有实参
    start_time = time.time() ###时间模块函数执行开始时间
    func()  ###实际上执行的是函数foo
    stop_time = time.time() ###时间模块函数执行结束时间
    print('函数运行的时间是%s' %(stop_time-start_time))
test(foo)
def foo(): ###执行函数foo,输出from the foo
    print('from the foo') 
def test(func): ###执行函数test返回一个func值,下面执行函数已经代入foo,则执行函数foo返回值
    return func

res = test(foo) ###函数test的内存地址赋值给变量res,最后执行res函数
res()
#闭包:在当前函数内找到的自己包中的变量,找不到的变量去外部找,函数作用域的体现
###################################################################################################################
#装饰器的实现:
# 1.定义一个函数(参数为另一个函数)
# 2.设定返回值,返回值为内部函数名称
# 3.定义内部函数,设定内部函数的方法
#####################################################################################################################
#装饰器的框架:
# def timer(func):
# def wrapper():
# # print(func)#嵌套的作用域
# strat_time = time.time()
# func()
# stop_time = time.time()
# print('运行时间是%s'%(stop_time-strat_time))
# return wrapper
####################################################################################################################
# import time
# @timer#作用是对函数进行计时
# def test():
# time.sleep(3)
# print('test函数运行完毕')
# timer(test)#返回的是wrapper的地址
# test = timer(test)
# test()
# test函数运行完毕
# 运行时间是3.0108001232147217
########################################加上返回值#####################################################
# def timer(func):
# def wrapper():
# # print(func)#嵌套的作用域
# strat_time = time.time()
# res = func()#赋值给func(),实际会赋值给test函数的一个返回值
# stop_time = time.time()
# print('运行时间是%s'%(stop_time-strat_time))
# return res#设定返回值
# return wrapper
# import time
# @timer#作用是对函数进行计时
# def test():
# time.sleep(3)
# print('test函数运行完毕')
# return 1
# # timer(test)#返回的是wrapper的地址
# # test = timer(test)
# res1 = test()
# print(res1)
#########################################加上参数######################################################
# def timer(func):
# def wrapper(*args,**kwargs):#默认可以输入任何参数
# # print(func)#嵌套的作用域
# strat_time = time.time()
# res = func(*args,**kwargs)#就是在运行test(),赋值给func(),实际会赋值给test函数的一个返回值
# stop_time = time.time()
# print('运行时间是%s'%(stop_time-strat_time))
# return res#设定返回值
# return wrapper
# import time
# @timer#test = timer(test)作用是对函数进行计时
# def test(name,age):
# time.sleep(3)
# print('test函数运行完毕,【名字是】:%s,【年龄是】:%s'%(name,age))
# return 1
# # timer(test)#返回的是wrapper的地址
# # test = timer(test)
# res1 = test('alex','18')#就是在运行wrapper
# print(res1)
#################################################################################################################
#解压序列 a,b,c = (1,2,3)数值是一一对应的关系
#交换解压序列
# a,b = (1,2)
# a,b = b,a
# print(a,b)
# 2 1
# user_dic = {'username':None,'login':False}
#
# def auth_func(func):
# def wrapper(*args,**kwargs):
# if user_dic['username'] and user_dic['login'] == True:
# res = func(*args, **kwargs)
# return res
# username = input('用户名:').strip()
# password = input('密码:').strip()
# if username =='pandaboy' and password =='123456':
# user_dic['username'] = username
# user_dic['login'] = True
# res = func(*args,**kwargs)
# return res
# else:
# print('用户名或密码错误')
# return wrapper
# @auth_func
# def index():
# print('欢迎来到主页')
# @auth_func
# def home(name):
# print('欢迎访问根目录%s'%name)
# def shopping_car():
# print('查询数据库里的内容')#需要访问列表中的数据库数据
# def order():
# print('查询订单内容')
# #给所有的函数加上验证功能
# index()
# home('alex')


 
原文地址:https://www.cnblogs.com/eddycomeon/p/8618510.html