三器是指装饰器,迭代器,生成器,一式是指列表生成式/推导式
一,装饰器
装饰器是三器中最重要的一器,它的作用就是:在不修改被装饰函数源代码的基础上,给其加上额外的功能。在python的web框架django等地方会起举足轻重的作用。
首先来看装饰器的标准写法
def add1(func):
def beau(*args, **kwargs):
v = func(*args, **kwargs)
return v
return beau
那么怎样使用呢?举个简单的栗子
import time
def add1(func):
def beau(*args, **kwargs):
print('This is start!')
v = func(*args, **kwargs) # 这是执行原函数并获取返回值
print('This is end!')
return v
return beau
@add1
def a1():
time.sleep(1)
print(123)
a1()
G:\Python38\python.exe G:/untitled/base/day10/pra3.py
This is start!
123
This is end!
Process finished with exit code 0
可能有人想说,咋感觉你这是多此一举呢?我直接写在函数里面不就更简单吗,那假如有几十个甚至更多的函数需要装饰呢?
带参数的装饰器
def add1(num):
def one(func):
def beau(*args, **kwargs):
v = func(*args, **kwargs)
return v
return beau
return one
举个简单的小栗子
# 带参数的装饰器
def add1(num):
def one(fun):
def beau(*args, **kwargs):
v = fun(*args, **kwargs)
print(num + 10)
return v
return beau
return one
@add1(5)
def func():
print(123)
func()
G:\Python38\python.exe G:/untitled/base/day10/pra3.py
123
15
Process finished with exit code 0
二,迭代器
对list,tuple,dict,str等数据类型使用for循环,从中依次取出数据,这个过程叫做遍历,也叫迭代。迭代是访问集合元素的一种常用的方式。
In a nutshell,可用for.循环的,就是可迭代对象。例如,list、tuple、dict、str就是可迭代对象。int、float、bool就是不可迭代的对象。
那上面巴拉巴拉了半天,what is 迭代器😭?
迭代器是一个可以记录遍历位置的对象,而且每次迭代都会返回下一条数据,直到取完为止,即把这个称为迭代器。
先上一个简单的栗子:
list1 = [23, 435, 5, 456, 67]
res = iter(list1)
print(res)
G:\Python38\python.exe G:/untitled/base/day12/prac1.py
<list_iterator object at 0x00000209A86318B0> <class 'list_iterator'>
Process finished with exit code 0
那么如何获取里面的元素呢?
list1 = [23, 435, 5, 456, 67]
res = iter(list1)
print(res.__next__())
print(res.__next__())
print(res.__next__())
G:\Python38\python.exe G:/untitled/base/day12/prac1.py
23
435
5
Process finished with exit code 0
额, 这样貌似是阔以的,但是好麻烦呀!可以用循环滴
list1 = [23, 435, 5, 456, 67]
res = iter(list1)
while True:
result = res.__next__()
print(result)
G:\Python38\python.exe G:/untitled/base/day12/prac1.py
Traceback (most recent call last):
File "G:/untitled/base/day12/prac1.py", line 21, in <module>
result = res.__next__()
StopIteration
23
435
5
456
67
Process finished with exit code 1
取是取出来了,但是因为取完后再取就会报错,所以加上异常处理
list1 = [23, 435, 5, 456, 67]
res = iter(list1)
while True:
try:
result = res.__next__()
print(result)
except Exception as e:
break
G:\Python38\python.exe G:/untitled/base/day12/prac1.py
23
435
5
456
67
Process finished with exit code 0
迭代器的另一种写法
st = 'hepengli'
val = st.__iter__()
print(val.__next__())
print(val.__next__())
G:\Python38\python.exe G:/untitled/base/day12/prac1.py
h
e
Process finished with exit code 0
三,生成器
先看一个生成器
def func():
yield 3
print('hello!')
yield 8
# 函数内部的代码不会执行,会返回一个生成器
result = func()
# print(result)
for i in result:
print(i)
G:\Python38\python.exe G:/untitled/base/day12/prac2.py
3
hello!
8
Process finished with exit code 0
感觉没啥特别的呀?
假如有海量的数据,用普通的方式一次性把数据直接干到内存中,那得有多奢侈呀?而生成器就帮你解决了这个脑阔疼的问题,它没有return, 取而代之的是使用yield语句一次返回一个结果,也就是一边循环一边计算的机制。
def func():
count = 1
while True:
if count <= 100000:
yield count
count += 1
else:
break
val = func()
for i in val:
print(i)
另一种形式
result = (i * i for i in range(10) if i > 5)
print(result)
G:\Python38\python.exe G:/untitled/base/day12/prac2.py
<generator object <genexpr> at 0x00000260B13BC9E0>
Process finished with exit code 0
四,列表生成式/推导式
输出100以内的偶数
一般的写法:
list1 = []
for i in range(100):
if i % 2 == 0:
list1.append(i)
print(list1)
使用列表生成式的写法:
print([i for i in range(100) if i % 2 == 0])
结果都为:
G:\Python38\python.exe G:/untitled/base/day12/prac3.py
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
Process finished with exit code 0
总之就是能简化代码,还是比较强大的!其实还有集合推导式和字典推导式,它们和列表推导式相似,有兴趣的看客朋友可以了解一二!