13.函数式编程:匿名函数、高阶函数、装饰器
# def add(x,y): # return x + y # print(add(1,2)) # 3# 匿名函数 # lambda表达式 # f = lambda x,y:x + y # print(f(1,2)) # # 三元表达式 # x = 2 # y = 1 # r = x if x > y else y # print(r) # 2
# map
# list_x = [1,2,3,4,5,6,7,8] # def square(x): # return x * x# 方法1 # for x in list_x: # square(x)# 方法2 # r = map(square,list_x) # print(r) # <map object at 0x029F31F0> # print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64]# r = map(lambda x:x*x,list_x) # print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64]# list_x = [1,2,3,4,5,6,7,8] # list_y = [1,2,3,4,5,6,7,8] # r = map(lambda x,y:x*x + y,list_x,list_y) # print(list(r)) # [2, 6, 12, 20, 30, 42, 56, 72]list_x = [1,2,3,4,5,6,7,8] list_y = [1,2,3,4,5,6] r = map(lambda x,y:x*x + y,list_x,list_y) print(list(r)) # [2, 6, 12, 20, 30, 42]
#reduce
from functools import reduce# 连续计算,连续调用lamdba list_x = [1,2,3,4,5,6,7,8] # r = reduce(lambda x,y:x+y,list_x) # print(r) # 36 # ((((((1+2)+3)+4)+5)+6)+7)+8 # x=1 # y=2# x=(1+2) # y=3# x=((1+2)+3) # y=4# x=(((1+2)+3)+4) # y=5 # ....# [(1,3),(2,-2),(-2,3),...]# r = reduce(lambda x,y:x+y,list_x,10) # print(r) # 46 # x=1 # y=10# x=(1+10) # y=2# x=((1+10)+2) # y=3# x=(((1+10)+2)+3) # y=4 # ....
# filter
list_x = [1,0,1,0,0,1] # r = filter(lambda x:True if x==1 else False,list_x) r = filter(lambda x:x,list_x) print(list(r)) # [1, 1, 1]list_u = ['a','B','c','F','e']
# 装饰器
import time def f1():print('this is a function_1')def f2():print('this is a function_2')def print_current_time(func):print(time.time())func()print_current_time(f1) # 1524300517.6711748 # this is a function_1 print_current_time(f2) # 1524300517.6711748 # this is a function_2
def decorator(func):def wrapper():print(time.time())func()return wrapperdef f1():print('this is a function_1') f = decorator(f1) f() # 1524301122.5020452 # this is a function_1
def decorator(func):def wrapper():print(time.time())func()return wrapper@decorator def f3():print('this is a function_2') f3() # 1524301486.9940615 # this is a function_2
@decorator def f1(func1):print('this is a function_name' + func1)@decorator def f2(func1,func2):print('this is a function_name' + func1)print('this is a function_name' + func2)f1('zouke1') # 1524302374.617277 # this is a function_namezouke1 f2('zouke3','zouke4') # 1524302374.6182854 # this is a function_namezouke3 # this is a function_namezouke4
def decorator(func):def wrapper(*args,**kw):print(time.time())func(*args,**kw)return wrapper@decorator def f3(func1,func2,**kw):print('this is a function_name' + func1)print('this is a function_name' + func2) f3('zouke3','zouke4',a=1,b=2,c='123') # 1524302802.5345788 # this is a function_namezouke3 # this is a function_namezouke4
@api.route('/get',methods=['GET']) def test_javascript_http():p = request.args.get('name')return p,200@api.route('/psw',method=['GET']) @auth.login_required def get_psw():p = request.args.get('psw')r = generate_password_hask(p)return 'aaaaa',200
转载于:https://www.cnblogs.com/zouke1220/p/8902030.html
13.函数式编程:匿名函数、高阶函数、装饰器相关推荐
- Python学习札记(二十) 函数式编程1 介绍 高阶函数介绍
参考: 函数式编程 高阶函数 Note A.函数式编程(Functional Programming)介绍 1.函数是Python内建支持的一种封装,我们通过一层一层的函数调用把复杂任务分解成简单的任 ...
- Scala编程基础——集合高阶函数
Scala编程基础--集合&高阶函数 集合 Scala中集合分为可变集合和不可变集合 可变集合:可以修改.添加.移除一个集合的元素. 不可变集合:安全的并发访问. 不可变集合,相比之下,永远不 ...
- Clickhouse 数组函数 高阶函数
数组函数的概述: Clickhouse> select version();SELECT version()┌─version()───┐ │ 20.8.1.4447 │ └────────── ...
- Kotlin 编程核心基石—高阶函数
前言 1. 高阶函数有多重要? 高阶函数,在 Kotlin 里有着举足轻重的地位.它是 Kotlin 函数式编程的基石,它是各种框架的关键元素,比如:协程,Jetpack Compose,Gradle ...
- 偏函数 匿名函数 高阶函数 map filter reduce
# 偏函数 创建一个新的函数,指定我们要使用的函数的某个参数为固定的值,这个新函数就是偏函数 def test(a, b, c, d=1):print(a + b + c + d)from funct ...
- python高阶函数filter_Python进阶系列连载(13)——Python内置高阶函数filter(上)...
前言 进阶部分连载继续~ 如果还没看过我的入门连载部分,先看: 当然,小编的免费入门课程已经有咯,看过连载的朋友可以看看视频再快速梳理一遍~ 前文传送门: filter filter是什么意思呢? 我 ...
- 廖雪峰讲python高阶函数-高阶函数
高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...
- Kotlin学习笔记 第三章 函数 高阶函数 lambda表达式 内联函数
参考链接 Kotlin官方文档 https://kotlinlang.org/docs/home.html 中文网站 https://www.kotlincn.net/docs/reference/p ...
- Python基础-函数,高阶函数
Python基础-函数 1.函数的定义和函数的执行流程: """ 函数学习 """print('程序开始') a = 10 # 在内存中申请 ...
最新文章
- maven-eclipse 中index.html页面乱码
- 自己做的压测实例(亲测)
- Python爬虫开发
- ch6 列表和导航条
- FastJson的常用操作
- C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)
- 木鸟民宿发布“中秋国庆出游住宿预测报告” 重庆、长沙最受欢迎
- linux下安装apache与php;Apache+PHP+MySQL配置攻略
- python最优调配问题_Python实现的基于优先等级分配糖果问题算法示例
- 反向代理服务器tengine学习小记
- ibm服务器安装2003系统,IBM X3650 M3服务器安装windows 2003的方法
- python音乐推荐系统的设计与实现_基于协同过滤的音乐推荐系统
- win10创建html,小米随身WIFI在WIN10下无法创建.htm
- 支付宝当面付打赏系统源码
- C# 标准ASCII 码表
- MySQL传统无损同步
- ioi 赛制_徐明宽IOI2017参赛总结及他的信息学竞赛之路
- PC端调用摄像头录制视频——vue标准写法
- 冰雪复古优化服务器,冰雪复古单职业:最适合长久稳定打金的传奇
- java clob 读取_java 实现读取clob
热门文章
- python2.0_day19_后台数据库设计思路
- Eclipse快捷键 10个最有用的快捷键---摘录
- [转]Java5泛型的用法,T.class的获取和为擦拭法站台
- JavaScript函数调用规则
- 一个不限制插值个数和上采样倍数的视频增强方法
- 近期海内外 AI 领域招聘、招生信息汇总
- 动态卷积:自适应调整卷积参数,显著提升模型表达能力 CVPR 2020
- Scikit-Learn 新版本发布!一行代码秒升级
- java的课程总结_Java课程总结
- C语言/C++程序员大神打造纯C的电子时钟(加图形库+源码)