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 1
闭包函数为函数加上认证功能
user_list=[
{'name':'alex','passwd':'123'},
{'name':'linhaifeng','passwd':'123'},
{'name':'wupeiqi','passwd':'123'},
{'name':'yuanhao','passwd':'123'},
{'name':'charon','passwd':'123'}
] current_dic={'username':None,'login':False}
def auth_func(func):
def wrapper(*args,**kwargs):
if current_dic['username'] and current_dic['login']:
res = func(*args,**kwargs)
return res
username = input('username is:').strip()
password = input('password is:').strip()
for usr_dic in user_list:
if username == usr_dic['name'] and password == usr_dic['passwd']:
current_dic['username'] = username
current_dic['login'] = True
res = func(*args,**kwargs)
return res
else:
print('username or password error')
return wrapper @auth_func
def index():
print('欢迎来到京东主页') @auth_func
def home(name):
print('欢迎回家%s' % name) @auth_func
def shopping_car(name):
print('%s的购物车里有[%s,%s,%s]' % (name, '奶茶', '妹妹', '娃娃')) print('before-->',current_dic)
index()
print('after--->',current_dic)
home('产品经理') 结果:
before--> {'username': None, 'login': False}
username is:charon
password is:123
欢迎来到京东主页
after---> {'username': 'charon', 'login': True}
欢迎回家产品经理
闭包函数为装饰函数加上参数
user_list=[
{'name':'alex','passwd':'123'},
{'name':'linhaifeng','passwd':'123'},
{'name':'wupeiqi','passwd':'123'},
{'name':'yuanhao','passwd':'123'},
]
current_dic={'username':None,'login':False} def auth(auth_type='filedb'):
def auth_func(func):
def wrapper(*args,**kwargs):
print('认证类型是',auth_type)
if auth_type == 'filedb':
if current_dic['username'] and current_dic['login']:
res = func(*args, **kwargs)
return res
username=input('用户名:').strip()
passwd=input('密码:').strip()
for user_dic in user_list:
if username == user_dic['name'] and passwd == user_dic['passwd']:
current_dic['username']=username
current_dic['login']=True
res = func(*args, **kwargs)
return res
else:
print('用户名或者密码错误')
elif auth_type == 'ldap':
print('鬼才特么会玩')
res = func(*args, **kwargs)
return res
else:
print('鬼才知道你用的什么认证方式')
res = func(*args, **kwargs)
return res return wrapper
return auth_func @auth(auth_type='filedb') #auth_func=auth(auth_type='filedb')-->@auth_func 附加了一个auth_type --->index=auth_func(index)
def index():
print('欢迎来到京东主页') @auth(auth_type='ldap')
def home(name):
print('欢迎回家%s' %name)
#
@auth(auth_type='sssssss')
def shopping_car(name):
print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃')) # print('before-->',current_dic)
# index()
# print('after--->',current_dic)
# home('产品经理')
shopping_car('产品经理')
day20 Python 高阶函数,函数,嵌套,闭包 装饰器的更多相关文章
- python 函数名 、闭包 装饰器 day13
1,函数名的使用. 函数名是函数的名字,本质就是变量,特殊的变量.函数名()加括号就是执行此函数. 1,单独打印函数名就是此函数的内存地址. def func1(): print(555) print ...
- python 全栈开发,Day11(函数名应用,闭包,装饰器初识,带参数以及带返回值的装饰器)
一.函数名应用 函数名是什么?函数名是函数的名字,本质:变量,特殊的变量. 函数名(),执行此函数. python 规范写法 1. #后面加一个空格,再写内容,就没有波浪线了. 2.一行代码写完,下面 ...
- Python笔记_第四篇_高阶编程_再议装饰器和再议内置函数
1. 概述: 我们在前面用了很多的装饰器这个工具的方法.这个位置要系统的讲一下装饰器. 1.2 为什么需要装饰器. 装饰器本质是一个Python函数,它可以让其他函数在不需要任何代码变动的前提下增加额 ...
- python之嵌套 闭包 装饰器 global、nonlocal关键字
嵌套: 在函数的内部定义函数闭包: 符合开放封闭原则:在不修改源代码与调用方式的情况下为函数添加新功能 # global 将局部变量变成全局变量 num = 100 def fn1(): globa ...
- Python四大神兽(迭代器&生成器&闭包&装饰器)
一.迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式.. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不 ...
- Python高阶函数及函数柯里化
1 Python高阶函数 接收函数为参数,或者把函数作为结果返回的函数为高阶函数. 1.1 自定义sort函数 要求:仿照内建函数sorted,自行实现一个sort函数.内建函数sorted函数是返回 ...
- python 高阶函数与装饰器
高阶函数定义1.函数接收的参数是一个函数名2.函数的返回值是一个函数名以上两者满足任意一个,就是高阶函数装饰器定义本质就是函数,功能是为其他函数添加新功能 装饰器的原则 1.不修改被装饰函数的源代码( ...
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- Python高阶函数_map/reduce/filter函数
本篇将开始介绍python高阶函数map/reduce/filter的用法,更多内容请参考:Python学习指南 map/reduce Python内建了map()和reduce()函数. 如果你读过 ...
随机推荐
- override与new的区别
using System; namespace ConsoleAppDemo { class BaseClass { public void Fun() { Console.WriteLine(&qu ...
- IIS部署wordpress4.7.4
准备环境和安装包:win7操作系统 iis php7.1.6 wordpress4.7.4 1.安装iis,需要cgi模块,一般安装建议全部勾选上. 2.安装配置php7.1.6 在官网http ...
- WebForm 【上传图片】【图片验证码】
上传图片(带水印) 1.获取要上传的图片 2.加水印 3.保存下来 using System.Drawing; --绘画类命名空间 图片最后要用绝对路径保存 Server.MapP ...
- 【github&&git】7、gitignore 修改不起作用
在git使用过程中有时会遇到修改了.gitignore文件,修改了之后发现,不能起作用,这是因为git存在缓存问题,所以做一下步骤即可: git rm -r --cached . git add . ...
- 通过swagger将API业务版本号与Gitlab代码版本号绑定
1.调用Gitlab API获取项目commit ID 2.编辑 Swagger2.java @Configuration @EnableSwagger2 @EnableWebMvc public c ...
- HDU4825(01字典树)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- 【代码笔记】Web-ionic-select
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 微信小程序转发功能
微信小程序转发涉及以下4个方法: 1.Page.onShareAppMessage({}) 设置右上角“转发”配置,及转发后回调函数返回 shareTicket 票据 2.wx.showSahreMe ...
- 去除git版本控制
命令:find . -name ".git" | xargs rm –Rf linux $ find . -type d -iname '__pycache__' -exec rm ...
- CentOS7系列--安装Chrome浏览器
CentOS7系列--安装Chrome浏览器 1. 创建yum源文件 [root@server20 ~]# cd /etc/yum.repos.d/ [root@server20 yum.repos. ...