栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

函数式编程

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

函数式编程

匿名函数

#匿名函数

def add(x,y):

    return x + y

print(add(1,2))

f = lambda x,y:x+y

print(f(1,2))

Python中的三元表达式: x if x > y else y

其它语言中的三元表达式:x > y ? x : y

map:其实就是一种映射。

如:

#map

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))

from functools import reduce

#连续计算,连续调用lambda

list_x = [1,2,3,4,5,6,7,8]

r = reduce(lambda x,y :x+y,list_x)

print(r)

map/reduce 编程模型 映射 归约  并行计算

函数式编程

filter:过滤

#filter

list_x = [1,0,1,0,0,1]

r = filter(lambda x:True if x==1 else False,list_x)

print(list(r))

#装饰器

import time

def f1():

    print(time.time())

    print('This is a function')

    

f1()

import time

def decorator(func):

    #key word

    def wrapper(*args,**kw):

        print(time.time())

        func(*args,**kw)  #是一种抽象的调用方式

    return wrapper

@decorator

def f1(func_name):

    print('This is a function named'+func_name)

@decorator

def f2(func_name1,func_name2):

    print('This is a function named'+func_name1)

    print('This is a function named'+func_name2)

@decorator

def f3(func_name1,func_name2,**kw):

    print('This is a function named'+func_name1)

    print('This is a function named'+func_name2)

    print(kw)

    

f3('test func1','test func3',a = 1,b = 2, c = '123')

@api.route('/get',methods = ['GET'])

def test_javascript_http():

    p = request.args.get('name')

    return p,200

@api.route('/psw',methods = ['GET'])

@auth.login_required

def get_psw():

    p = request.args.get('pws')

    r = generate_password_hash(p)

    return 'aaaaaa',200

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/329286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号