装饰器
def assist(item):
def inner():
print("成为伙伴")
item()
print("完结")
return inner
@assist # 相当于xxx = assist(EVA)
def EVA():
print("一起成为EVA驾驶员")
@assist # 相当于xxx = assist(One_Piece)
def One_Piece():
print("一起成为海贼王")
EVA() # 执行的是内层函数inner
One_Piece()
print("------------------------------------------")
def assist(item):
def inner(*args, **kwargs):
print("登录")
item(*args, **kwargs)
print("退出")
return inner
@assist
def play_steam(username, pwd, game):
print("我要开启steam了.", username, pwd, game)
@assist
def play_gamer(username, pwd):
print("我要玩游戏了.", username, pwd)
play_gamer("admin", "123456")
play_steam("admin", "123456", "艾尔登")
print("------------------------------------------")
# 通用装饰器写法(接收返回值):
def assist(item):
def inner(*args, **kwargs):
print("登录")
ret = item(*args, **kwargs)
print("退出")
return ret
return inner
@assist
def play_steam(username, pwd, game):
print("我要开启steam了.", username, pwd, game)
return "买个新游戏"
@assist
def play_gamer(username, pwd):
print("我要玩游戏了.", username, pwd)
play_gamer("admin", "123456")
ret = play_steam("admin", "123456", "艾尔登")
print(ret)
print("------------------------------------------")
# 一个函数可以被多个装饰器装饰
def wrapper1(item):
def inner(*args, **kwargs):
print("这里是wrapper1进入")
ret = item(*args, **kwargs)
print("这里是wrapper1退出")
return ret
return inner
def wrapper2(item):
def inner(*args, **kwargs):
print("这里是wrapper2进入")
ret = item(*args, **kwargs)
print("这里是wrapper2退出")
return ret
return inner
@wrapper1 # 装饰顺序:wrapper1(wrapper2(target))
@wrapper2
def target():
print("我是目标")
target()
"""
这里是wrapper1进入
这里是wrapper2进入
我是目标
这里是wrapper2退出
这里是wrapper1退出
"""
一个简单的装饰器例子
flag = False # 添加判断状态,输入成功一次,后续的操作不用重复登录
def login_verify(item):
def inner(*args, **kwargs):
global flag # 用于下面修改全局变量
if flag == False:
while 1:
username = input("请输入用户名>>>")
pwd = input("请输入密码>>>")
if username == "admin" and pwd == "123456":
print("登陆成功")
flag = True
break
else:
print("用户名或密码失败,请重新输入")
ret = item(*args, **kwargs)
return ret
return inner
@login_verify
def add():
print("我要进行添加")
@login_verify
def update():
print("我要进行修改")
@login_verify
def search():
print("我要查询")
add()
update()
search()
"""
请输入用户名>>>admin
请输入密码>>>123456
登陆成功
我要进行添加
我要进行修改
我要查询
"""
迭代器
"""
iterable:可迭代的
str,list,tuple,dict,set,open()
可迭代的数据类型都会提供一个迭代器,可以把数据类型中的所有数据逐一取出
"""
it = iter("我要成为海贼王")
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了"))
print(next(it, "没了")) # 若超出范围,则显示指定内容来代替报错
it = "我驾驶EVA".__iter__()
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print("---------------------------------")
# 模拟for循环工作原理
s = "我喜欢看动漫"
it = s.__iter__()
while 1:
try:
data = it.__next__()
print(data)
except StopIteration:
break
print("结束")
# for循环里面一定是要拿迭代器的,所以不可迭代的对象不能用for循环,例如int类型
# 迭代器统一了不同数据类型的遍历工作
print("---------------------------------")
# 迭代器本身也是可迭代的
s = "我喜欢打游戏"
it = s.__iter__()
for i in it:
print(i)
生成器
# 生成器: 本质是迭代器
def func():
print("aaa")
yield 123 # 与return效果相似,只要函数中出现了yield,就是生成器函数
# 生成器函数执行时,不会执行函数,得到的是一个生成器
ret = func()
print(ret)
print(ret.__next__()) # 只有执行next的时候,yield才会返回数据
print("------------------------------------------")
def func():
print("aa")
yield 11 # 与return不同,不会停止下面的代码
print("bb")
yield 22
print("cc")
yield 33
ret = func()
print(ret.__next__()) # aa 11
print(ret.__next__()) # bb 22
print(ret.__next__()) # cc 33
print("------------------------------------------")
def order():
lst = []
for i in range(100):
lst.append(f"漫画{i}")
if len(lst) == 10: # 10次一拿
yield lst
# 下一次从这个位置开始
lst = [] # 下一次拿清空
gen = order()
print(gen.__next__())
print(gen.__next__())
print(gen.__next__())
print("---------------------------------")
# 生成器表达式
gen = (i for i in range(10))
# print(gen.__next__())
# print(gen.__next__())
# print(gen.__next__())
# for i in gen:
# print(i)
print(list(gen))
推导式
lst = []
for i in range(10):
lst.append(i)
print(lst)
# 推导式简化代码
lst = [i for i in range(10)]
print(lst)
# lst = [i for i in range(1, 10, 2)]
lst = [i for i in range(10) if i % 2 == 1]
print(lst)
lst = [f"漫画{i}" for i in range(50)]
print(lst)
lst = ["asuka", "iroha", "asuna"]
# 都变成大写
lst2 = [item.upper() for item in lst]
print(lst2)
print("---------------------------------------")
#集合推导式
s = {i for i in range(10)}
print(s)
#字典推导式
lst = ["asuka", "iroha", "asuna", "gundam"]
dic = {i:lst[i] for i in range(len(lst))}
print(dic)
#没有元组推导式