python 高阶函数与装饰器
- 高阶函数定义
1.函数接收的参数是一个函数名
2.函数的返回值是一个函数名
以上两者满足任意一个,就是高阶函数- 装饰器定义
本质就是函数,功能是为其他函数添加新功能
- 装饰器的原则
1.不修改被装饰函数的源代码(开放封闭原则)
2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式
装饰器=高阶函数+函数嵌套+闭包
- # 无返回值无参数
- import time
- def timer(func): #func = test
- def w():
- start_time = time.time()
- func() #就是在运行test()
- stop_time = time.time()
- print("运行时间是%d"%(stop_time-start_time))
- return w
- @timer # 相当于 test = timer(test)
- def test():
- time.sleep(2)
- print("from test")
- # test = timer(test) #返回的是w的地址
- # test() #相当于执行w
- test()
- #加上返回值
- import time
- def timer(func): #func = test
- def w():
- start_time = time.time()
- res = func() #就是在运行test()
- stop_time = time.time()
- print("运行时间是%d"%(stop_time-start_time))
- return res
- return w
- @timer # 相当于 test = timer(test)
- def test():
- time.sleep(2)
- print("from test")
- return "这是test的返回值"
- # test = timer(test) #返回的是w的地址
- # test() #相当于执行w
- res = test()
- print(res)
- #加上参数和返回值 装饰器最终形式
- import time
- def timer(func): #func = test #func = test1
- def w(*args,**kwargs):
- start_time = time.time()
- res = func(*args,**kwargs) #就是在运行test() test1()
- stop_time = time.time()
- print("运行时间是%d"%(stop_time-start_time))
- return res
- return w
- @timer # 相当于 test = timer(test)
- def test(name,age):
- time.sleep(2)
- print("from test %s %s"%(name,age))
- return "这是test的返回值"
- res = test("liao",18)
- print(res)
- @timer # 相当于 test1 = timer(test1)
- def test1(name,age,g):
- time.sleep(1)
- print("from test1 %s %s %s"%(name,age,g))
- return "这是test1的返回值"
- res1 = test1("bo",26,"shi")
- print(res1)
用户登陆(简单流程判断)
- l = [{"name":"liao","pwd":"123"},{"name":"tom","pwd":"123"}] #用户数据
- c_d = {"user":None,"login":False} #定义一个空的临时的用户字字典
- def a_f(func):
- def w(*args,**kwargs):
- if c_d["user"] and c_d["login"]: #判断临时字典里是否有用户登陆,没有就输入
- res = func(*args,**kwargs) #有就进入下一步
- return res
- user = input("请输入用户名:").strip() #临时字典没有数据就输入用户名
- pwd = input("请输入密码:").strip() #临时字典没有数据就输入密码
- for i in l: #遍历用户数据
- if user == i["name"] and pwd == i["pwd"]: #判断输入的用户和密码是否在用户数据里
- c_d["user"] = user #输入正确,数据保存到临时的用户字字典里,下一步不用再输入用户和密码
- c_d["login"] = True
- res = func(*args,**kwargs) #进入
- return res
- else: #如果输入的用户名和密码不在用记数据里,提示用户
- print("用户名或者密码错误")
- return w
- @a_f
- def index():
- print("欢迎来到主页面")
- @a_f
- def home():
- print("这里是你家")
- @a_f
- def shopping_car():
- print("查看购物车啊亲")
- index()
- home()
- shopping_car()
python 高阶函数与装饰器的更多相关文章
- Python高阶函数之 - 装饰器
高阶函数: 1. 函数名可以作为参数传入 2. 函数名可以作为返回值. python装饰器是用于拓展原来函数功能的一种函数 , 这个函数的特殊之处在于它的返回值也是一个函数 , 使用pyth ...
- Python学习笔记【第六篇】:迭代器、生成器、高阶函数、装饰器
迭代器 迭代器是访问集合元素的一种方式,迭代器从对象的第一个元素开始访问,知道所有元素被访问完成.迭代器只能往前访问,不能通过索引访问. 类型内部使用__iter__()方法转为迭代器,使用__nex ...
- python开发基础04-函数、递归、匿名函数、高阶函数、装饰器
匿名函数 lamba lambda x,y,z=1:x+y+z 匿名就是没有名字 def func(x,y,z=1): return x+y+z 匿名 lambda x,y,z=1:x+y+z #与函 ...
- python笔记十三(高阶函数、装饰器)
一.高阶函数 函数只要有以下两个特征中一个就可以称为高阶函数: a:函数名作为一个实参传入另一个函数中 b:函数的返回值中包含函数名 下面我们用代码来感受一下这两种形式: import time # ...
- Python_高阶函数、装饰器(decorator)
一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...
- python中高阶函数与装饰器
高阶函数的定义:传入参数有函数名或者返回值有内置函数名的函数. 最简单的高阶函数: def add(x, y, f): return f(x) + f(y) add(-5, 6, abs) 常用 ...
- Python3(十) 函数式编程: 匿名函数、高阶函数、装饰器
一.匿名函数 1.定义:定义函数的时候不需要定义函数名 2.具体例子: #普通函数 def add(x,y): return x + y #匿名函数 lambda x,y: x + y 调用匿名函数: ...
- Python(十) 函数式编程: 匿名函数、高阶函数、装饰器
一.lambda表达式 lambda parameter_list: expression # 匿名函数 def add(x,y): return x+y print(add(1,2)) f = la ...
- python中高阶函数与装饰器(3)
>>> f = lambda x: x * x>>> f<function <lambda> at 0x101c6ef28> >> ...
随机推荐
- Oracle Commit 方式 COMMIT WRITE batch NOWAIT;
1111 CREATE OR REPLACE PROCEDURE update_hav_tpnd IS CURSOR hav_tpnd_cur IS SELECT d.hav_tpnd, d. ...
- SQL 之witn as语法
with as 是临时视图的语法:with qry_a as (select * from table_a )select * from qry_a ;
- [Appium] 使用Appium过程中遇到的各种坑
以下问题都是以ios为背景: 1. 问题: Case: 在页面S1上,点击元素A后,判读B元素是否出现. Detail:一开始通过Appium Inspector, 可以找到B元素,所以直接取该元素的 ...
- EXT学习之——EXT下拉框默认绑定第一个值
//默认第一个下拉框绑定值if (this.moduleCombo.store.getAt(0) != undefined) { this.moduleCombo.setValue(this.modu ...
- Ubuntu 12.10使用apt安装Oracle/Sun JDK
apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java sudo apt-get ...
- 【MySQL】MySQL 5.7+ 版本的初始化
MySQL 5.7.7以上二进制包就不包括原data目录的初始化系统表,官网说明: http://dev.mysql.com/doc/refman/5.7/en/data-directory-init ...
- PIC32MZ tutorial -- Change Notification
In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...
- node,不懂不懂
Four Day-------------------------node.js分对象全局/核心模块/文件模块path(核心模块)--作用:操作路径basername/获取传入路劲dimame/获取传 ...
- A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war
完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...
- linux C学习笔记02--共享内存(进程同步)
system V下3中进程同步:共享内存(shared memory),信号量(semaphore)和消息队列(message queue) 调试了下午,终于调通啦! 运行./c.out 输出共享内存 ...