什么是匿名函数,就是在定的时时候,我们不需要定义函数名
# 匿名函数
def add(x,y):
return x+y
lambda parameter_list: expression
# parameter_list 等价于函数参数x,y # expression 等价于函数定义部分
lambda x,y: x+y
f = lambda x,y: x+y
a = f(1, 2)
print(a)
# 结果:
3
f = lambda x,y: a = x+y
a = f(1, 2)
print(a)
# 结果:
f = lambda x,y: a = x+y
^
SyntaxError: cannot assign to lambda
# 判断条件 ? 结果真:结果为假
# 条件为真时返回结果 if 条件判断 else 条件为假返回结果
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 squarte(x):
return x * x
r = map(squarte, list_x)
print(r)
# 结果:
<map object at 0x00000234EE8C35E0>
# 转换成列表
print(list(r))
# 结果:
[1, 4, 9, 16, 25, 36, 49, 64]
"""---------------------- map和lambda ----------------------"""
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
r = map(lambda x:x*x,list_x)
print(list(r))
# 结果:
[1, 4, 9, 16, 25, 36, 49, 64]
map可以传入多个变量
"""---------------------- map和lambda ----------------------"""
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]
def reduce(function, sequence, initial = None)
# function 函数
# sequence 序列
# initial 初始值
reduce
里面传入的函数必须有两个参数
连续计算、连续调用lambda、每一次的lambda表达式的计算结果都会作为下一次的第一个变量传入进去
"""---------------------- reduce ----------------------"""
from functools import reduce
# 导入reduce
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
r = reduce(lambda x,y:x+y,list_x)
print(r)
# 结果:((1+2)+3)...+8
36
"""---------------------- reduce ----------------------"""
from functools import reduce
# 导入reduce
list_x = ["1", "2", "3", "4", "5", "6", "7", "8"]
r = reduce(lambda x,y:x+y,list_x,"aaa")
print(r)
# 运行结果:
aaa12345678
"""---------------------- filter ----------------------"""
list_x = [1, 0, 1, 0, 1, 0, 1, 1]
r = filter(lambda x:True if x==1 else False,list_x)
print(list(r))
# 结果:
[1, 1, 1, 1, 1]
特点:lambda
表达式的结果必须是ture
或false
"""---------------------- 装饰器 ----------------------"""
import time
def decorator(func):
def wrapper():
print(time.time())
func()
return wrapper
def f1():
print("This is a function")
f = decorator(f1)
f()
# 结果:
1603359509.3139493
This is a function
语法塘@
"""---------------------- 装饰器 ----------------------"""
import time
# 装饰器
def decorator(func):
"""传入函数参数"""
def wrapper():
print(time.time())
func()
return wrapper
@decorator
def f1():
print("This is a function")
f1()
# 运行结果:
1603360018.837507
This is a function
接受定义时的复杂,不接受调用时的复杂,添加功能时,不改变原函数
在函数中添加参数
"""---------------------- 装饰器 ----------------------"""
import time
def decorator(func):
"""传入函数参数"""
def wrapper(function_name):
print(time.time())
func(function_name)
return wrapper
@decorator
def f1(function_name):
print("This is a function"+function_name)
f1("\tf1")
# 结果:
1603363176.543661
This is a function f1
可变参数,解决函数参数个数不同的问题
"""---------------------- 装饰器 ----------------------"""
import time
def decorator(func):
"""传入函数参数"""
def wrapper(*args):
print(time.time())
func(*args)
return wrapper
@decorator
def f1(function_name, function_name2):
print("This is a function"+function_name+function_name2)
f1("\tf1","\tf2")
# 结果:
1603363428.571781
This is a function f1 f2
"""---------------------- 装饰器 ----------------------"""
import time
def decorator(func):
"""传入函数参数"""
def wrapper(*args,**kw):
print(time.time())
func(*args,**kw)
return wrapper
@decorator
def f3(function_name, function_name2,**kw):
print("This is a function"+function_name+function_name2)
print(kw)
f3("\ta","\ts",a=1,c='123')
# 结果:
1603363822.756178
This is a function a s
{'a': 1, 'c': '123'}
rgs,**kw)
return wrapper
@decorator
def f3(function_name, function_name2,**kw):
print(“This is a function”+function_name+function_name2)
print(kw)
f3("\ta","\ts",a=1,c=‘123’)
1603363822.756178
This is a function a s
{‘a’: 1, ‘c’: ‘123’}
### 4.装饰器四,应用
+ 对封装的单元进行修改的话,可以通过装饰器的形式进行修改。