functools itertools知识点
import functools
# 使用functools改变装饰器指向
def auth(func):
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
@auth
def login():
pass
print(login.__name__)
"""
inner
"""
# 可以使用functools改变装饰器指向
import functools
def auth(func):
@functools.wraps(func)
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
@auth
def login():
pass
print(login.__name__)
"""
login
"""
# 多个装饰器的执行顺序
# 离函数最近的装饰器最先执行,由内向外
import functools
def auth1(func):
# print("auth1")
@functools.wraps(func)
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
def auth0(func):
# print("auth0")
@functools.wraps(func)
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner
@auth0
@auth1
def login():
pass
login()
"""
auth1
auth0
"""
# 实际上被包裹上之后,执行的函数变成了inner,但是名字还是login
# 偏函数
import functools
def func(a1, a2):
print(a1, a2)
new_func = functools.partial(func, 123)
new_func(456)
123 456
import itertools
v1 = [11, 22, 33]
v2 = [44, 55, 66]
v = itertools.chain(v1, v2)
for item in v:
print(item)
# 11
# 22
# 33
# 44
# 55
# 66