高阶函数定义
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 高阶函数与装饰器的更多相关文章

  1. Python高阶函数之 - 装饰器

    高阶函数:  1. 函数名可以作为参数传入     2. 函数名可以作为返回值. python装饰器是用于拓展原来函数功能的一种函数 , 这个函数的特殊之处在于它的返回值也是一个函数 , 使用pyth ...

  2. Python学习笔记【第六篇】:迭代器、生成器、高阶函数、装饰器

    迭代器 迭代器是访问集合元素的一种方式,迭代器从对象的第一个元素开始访问,知道所有元素被访问完成.迭代器只能往前访问,不能通过索引访问. 类型内部使用__iter__()方法转为迭代器,使用__nex ...

  3. 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 #与函 ...

  4. python笔记十三(高阶函数、装饰器)

    一.高阶函数 函数只要有以下两个特征中一个就可以称为高阶函数: a:函数名作为一个实参传入另一个函数中 b:函数的返回值中包含函数名 下面我们用代码来感受一下这两种形式: import time # ...

  5. Python_高阶函数、装饰器(decorator)

    一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...

  6. python中高阶函数与装饰器

    高阶函数的定义:传入参数有函数名或者返回值有内置函数名的函数. 最简单的高阶函数: def add(x, y, f):    return f(x) + f(y) add(-5, 6, abs) 常用 ...

  7. Python3(十) 函数式编程: 匿名函数、高阶函数、装饰器

    一.匿名函数 1.定义:定义函数的时候不需要定义函数名 2.具体例子: #普通函数 def add(x,y): return x + y #匿名函数 lambda x,y: x + y 调用匿名函数: ...

  8. Python(十) 函数式编程: 匿名函数、高阶函数、装饰器

    一.lambda表达式 lambda parameter_list: expression # 匿名函数 def add(x,y): return x+y print(add(1,2)) f = la ...

  9. python中高阶函数与装饰器(3)

    >>> f = lambda x: x * x>>> f<function <lambda> at 0x101c6ef28> >> ...

随机推荐

  1. ios crash 日志分析

    以下内容来自网络 https://coderwall.com/p/ezdcmg/symbolicating-an-ios-crash-log-without-the-original-dsym-fil ...

  2. Centos6.4 用rpm方式安装MySql5.6

    1.查看系统是否安装了MySQL     使用命令:     #rpm -qa | grep mysql    2.卸载已安装的MySQL      卸载mysql命令如下:       #rpm - ...

  3. datatable list 之前相互转换

    使用 FastMember: IEnumerable<SomeType> data = ... DataTable table = new DataTable(); using(var r ...

  4. 【转】windows7 修改环境变量 和 用不用重启电脑的讨论

      原文:http://www.cnblogs.com/zhenmingliu/archive/2013/02/21/2921396.html   先到我的电脑>属性>高级>环境变量 ...

  5. Android软件测试Monkey测试工具

    前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.其 ...

  6. 精妙SQL语句

    asc 按升序排列desc 按降序排列 下列语句部分是Mssql语句,不可以在access中使用.SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据 ...

  7. how to monitor system logs and export to files simultaneously

    What will you do when you conduct a malware analysis on a smartphone? You will focus on running proc ...

  8. netstat大量time_wait连接

    http://chembo.iteye.com/blog/1503770 http://www.2cto.com/os/201007/54067.html http://blog.csdn.net/d ...

  9. UVa 458 - The Decoder

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  10. VC++ chap12 file

    file operation _______C语言对文件操作的支持 fopen accepts paths that are valid on the file system at the point ...