# 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.函数式编程:匿名函数、高阶函数、装饰器相关推荐

  1. Python学习札记(二十) 函数式编程1 介绍 高阶函数介绍

    参考: 函数式编程 高阶函数 Note A.函数式编程(Functional Programming)介绍 1.函数是Python内建支持的一种封装,我们通过一层一层的函数调用把复杂任务分解成简单的任 ...

  2. Scala编程基础——集合高阶函数

    Scala编程基础--集合&高阶函数 集合 Scala中集合分为可变集合和不可变集合 可变集合:可以修改.添加.移除一个集合的元素. 不可变集合:安全的并发访问. 不可变集合,相比之下,永远不 ...

  3. Clickhouse 数组函数 高阶函数

    数组函数的概述: Clickhouse> select version();SELECT version()┌─version()───┐ │ 20.8.1.4447 │ └────────── ...

  4. Kotlin 编程核心基石—高阶函数

    前言 1. 高阶函数有多重要? 高阶函数,在 Kotlin 里有着举足轻重的地位.它是 Kotlin 函数式编程的基石,它是各种框架的关键元素,比如:协程,Jetpack Compose,Gradle ...

  5. 偏函数 匿名函数 高阶函数 map filter reduce

    # 偏函数 创建一个新的函数,指定我们要使用的函数的某个参数为固定的值,这个新函数就是偏函数 def test(a, b, c, d=1):print(a + b + c + d)from funct ...

  6. python高阶函数filter_Python进阶系列连载(13)——Python内置高阶函数filter(上)...

    前言 进阶部分连载继续~ 如果还没看过我的入门连载部分,先看: 当然,小编的免费入门课程已经有咯,看过连载的朋友可以看看视频再快速梳理一遍~ 前文传送门: filter filter是什么意思呢? 我 ...

  7. 廖雪峰讲python高阶函数-高阶函数

    高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...

  8. Kotlin学习笔记 第三章 函数 高阶函数 lambda表达式 内联函数

    参考链接 Kotlin官方文档 https://kotlinlang.org/docs/home.html 中文网站 https://www.kotlincn.net/docs/reference/p ...

  9. Python基础-函数,高阶函数

    Python基础-函数 1.函数的定义和函数的执行流程: """ 函数学习 """print('程序开始') a = 10 # 在内存中申请 ...

最新文章

  1. maven-eclipse 中index.html页面乱码
  2. 自己做的压测实例(亲测)
  3. Python爬虫开发
  4. ch6 列表和导航条
  5. FastJson的常用操作
  6. C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)
  7. 木鸟民宿发布“中秋国庆出游住宿预测报告” 重庆、长沙最受欢迎
  8. linux下安装apache与php;Apache+PHP+MySQL配置攻略
  9. python最优调配问题_Python实现的基于优先等级分配糖果问题算法示例
  10. 反向代理服务器tengine学习小记
  11. ibm服务器安装2003系统,IBM X3650 M3服务器安装windows 2003的方法
  12. python音乐推荐系统的设计与实现_基于协同过滤的音乐推荐系统
  13. win10创建html,小米随身WIFI在WIN10下无法创建.htm
  14. 支付宝当面付打赏系统源码
  15. C# 标准ASCII 码表
  16. MySQL传统无损同步
  17. ioi 赛制_徐明宽IOI2017参赛总结及他的信息学竞赛之路
  18. PC端调用摄像头录制视频——vue标准写法
  19. 冰雪复古优化服务器,冰雪复古单职业:最适合长久稳定打金的传奇
  20. java clob 读取_java 实现读取clob

热门文章

  1. python2.0_day19_后台数据库设计思路
  2. Eclipse快捷键 10个最有用的快捷键---摘录
  3. [转]Java5泛型的用法,T.class的获取和为擦拭法站台
  4. JavaScript函数调用规则
  5. 一个不限制插值个数和上采样倍数的视频增强方法
  6. 近期海内外 AI 领域招聘、招生信息汇总
  7. 动态卷积:自适应调整卷积参数,显著提升模型表达能力 CVPR 2020
  8. Scikit-Learn 新版本发布!一行代码秒升级
  9. java的课程总结_Java课程总结
  10. C语言/C++程序员大神打造纯C的电子时钟(加图形库+源码)