第二节
第二节 常用模块
1.装饰器
定义:本质是函数(装饰其他函数)就是为其他函数添加附加功能。
原则:
1. 不能修改被装饰的函数的源代码
2. 不能修改被装饰的函数的调用方式
装饰器知识储备:
1. 函数即“变量”
2. 高阶函数
a. 把一个函数名当作实参传给另外一个函数(在不修改被装饰函数源代码的情况下为其添加功能)
b. 返回值中包含函数名(不修改函数的调用方式)
3. 嵌套函数
定义基本装饰器:
###### 基本装饰器 ######
def orter(func): #定义装饰器
def inner():
print("This is inner before.")
s = func() #调用原传入参数函数执行
print("This is inner after.")
return s #return原函数返回值
return inner #将inner函数return给name函数
@orter #调用装饰器(将函数name当参数传入orter装饰器)
def name():
print("This is name.")
return True #name原函数return True
ret = name()
print(ret)
输出结果:
This is inner before.
This is name.
This is inner after.
True
给装饰器传参数:
###### 装饰器传参数 ######
def orter(func):
def inner(a,b): #接收传入的2个参数
print("This is inner before.")
s = func(a,b) #接收传入的2个参数
print("This is inner after.")
return s
return inner
@orter
def name(a,b): #接收传入的2个参数,并name整体函数当参数传入orter装饰器
print("This is name.%s,%s"%(a,b))
return True
ret = name('nick', 'jenny') #传入2个参数
print(ret)
输出结果:
This is inner before.
This is name.nick,jenny
This is inner after
True
给装饰器传万能参数:
########## 万能参数装饰器 ##########
def orter(func):
def inner(*args,**kwargs): #万能参数接收多个参数
print("This is inner before.")
s = func(*args,**kwargs) #万能参数接收多个参数
print("This is inner after.")
return s
return inner
@orter
def name(a,b,c,k1='nick'): #接受传入的多个参数
print("This is name.%s,%s"%(a,b))
return True
ret = name('nick','jenny','car')
print(ret)
输出结果:
This is inner before.
This is name.nick,jenny
This is inner after.
True
一个函数应用多个装饰器方法:
########### 一个函数应用多个装饰器 #########
def orter(func):
def inner(*args,**kwargs):
print("This is inner one before.")
print("This is inner one before angin.")
s = func(*args,**kwargs)
print("This is inner one after.")
print("This is inner one after angin.")
return s
return inner
def orter_2(func):
def inner(*args,**kwargs):
print("This is inner two before.")
print("This is inner two before angin.")
s = func(*args,**kwargs)
print("This is inner two after.")
print("This is inner two after angin.")
return s
return inner
@orter #将以下函数整体当参数传入orter装饰器
@orter_2 #将以下函数当参数传入orter_2装饰器
def name(a,b,c,k1='nick'):
print("This is name.%s and %s."%(a,b))
return True
ret = name('nick','jenny','car')
print(ret)
输出结果:
This is inner one before.
This is inner one before angin.
This is inner two before.
This is inner two before angin.
This is name.nick and jenny.
This is inner two after.
This is inner two after angin.
This is inner one after.
This is inner one after angin.
True
2.迭代器&生成器
迭代器
迭代器只不过是一个实现迭代协议的容器对象。
特点:
1.访问者不需要关心迭代器内部结构,仅需通过next()方法不断去取下一个内容
2.不能随机访问集合中的某个值,只能从头到尾依次访问
3.访问到一半时不能往回退
4.便于循环比较大的数据集合,节省内存
>>> a = iter([1,2,3,4,5])
>>> a
<list_iterator object at 0x101402630>
>>> a.__next__()
1
>>> a.__next__()
2
>>> a.__next__()
3
>>> a.__next__()
4
>>> a.__next__()
5
>>> a.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration #末尾生成StopIteration异常
生成器
一个函数被调用时返回一个迭代器,那这个函数就是生成器(generator);如果函数中包含yield语法,那这个函数就会变成生成器。
def xran():
print("one")
yield 1
print("two")
yield 2
print("sr")
yield 3
ret = xran()
#print(ret) #<generator object xran at 0x00000000006ED308>
result = ret.__next__()
print(result)
result = ret.__next__()
print(result)
result = ret.__next__()
print(result)
# ret.__next__() #循环完毕抛出StopIteration
#
# ret.close() #关闭生成器
生成器表达式:
>>>a=[7,8,9]
>>>b=[i**2 for i in a]
>>>b
[49, 64, 81]
>>>ib=(i**2 for i in a)
>>>ib
<generator object <genexpr> at 0x7f72291217e0>
>>>next(ib)
49
>>>next(ib)
64
>>>next(ib)
81
>>>next(ib)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

浙公网安备 33010602011771号