匿名函数
#匿名函数
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