前言
本文章主要介绍Python中的一些高级用法,这些用法在工作和面试中都是及其重要的。能够让我们深入的去理解Python和提高我们的编码效率。重要的是有逼格和面试也是常见问题。
迭代器
生成器
装饰器
迭代器
在迭代器这里,有一个重要的区分点是在于,可迭代对象和迭代器的区别。
什么是可迭代对象呢?
什么是迭代器呢?
迭代器和可迭代对象又有什么关系呢?
下面我直接出一张图来介绍一下他们的关系吧!
以下是结论
1)可迭代对象包含迭代器
2)如果一个对象拥有iter方法,其是可迭代对象;如果一个对象拥有next方法,其就是迭代器
3)定义可迭代对象,必须实现*iter方法;定义迭代器,必须实现iter_方法和*next_方法
来了来了,我们接下来用代码去实现一下迭代器的用法
先举一个简单的实现:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n136">// 以下代码在IDLE中输出
i = iter('abc')
// iter函数与iter方法联系非常紧密,iter()是直接调用该对象的iter(),并把iter()的返回结果作为自己的返回值,故该用法常被称为“创建迭代器”。
next(i)
'a'
next(i)
'b'
next(i)
'c'
next(i)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
next(i)
StopIteration
</pre>
当遍历完序列时,会引发一个StopIteration异常。这样迭代器就可以与循环兼容,因为可以捕获这个异常并停止循环。
你以为这样就没了嘛?!!!!
最后我们来自己实现一个迭代器吧!
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python " style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n140">class CountDown:
def init(self, step):
self.step = step
def next(self):
"""Return the next element."""
if self.step <= 0:
raise StopIteration
self.step -= 1
return self.step
def iter(self):
"""Return the iterrator itself"""
return self</pre>
生成器
我们从迭代器那可以知道,生成器其实也是一种迭代器,它是一种特殊的迭代器,它自动的实现了“迭代器协议”(即iter和next方法)
)那我们如果去定义一个生成器呢?
只要Python函数的定义体中有yield关键字,该函数就是生成器函数。调用生成器函数时,会返回一个生成器对象,也就是说,生成器函数时生成器工厂
我们来举一个栗子(找一个栗子的图)
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n148">def fibonacci():
a, b = 0, 1
while True:
yield b
a, b = b, a + b
fib = fibonacci()
next(fib)
1
next(fib)
1
next(fib)
2
next(fib)
3
next(fib)
5
next(fib)
8
[next(fib) for i in range(10)]
[13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
// 我们可以使用next()函数或者for循环从生成器中获取新的元素,就像迭代器一样
</pre>
我们举了一个斐波那契数列,它时一个无穷的数列,但我们用了生成器去实现,我们每次就提供了一个值,这样就不用无限大的内存。我能想到一个应用场景就是使用生成器的数据流缓冲区。使用这些数据的第三方代码可以展厅,恢复和停止生成器,在一开始这一过程之前,我们无需导入所有数据。
我们再来一个栗子吧,我们来实现一个等差数列生成器
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n151">class ArithmeticProgression:
def init(self, begin, step, end=None):
self.begin = begin
self.step = step
self.end = end # None -> 无穷数列
def iter(self):
// 把self.begin赋值给result,不过会先强制转换成前面的假发算式得
到的类型
result = type(self.begin + self.step)(self.begin)
forever = self.end is None
index = 0
while forever or result < self.end:
yield result
index += 1
result = self.begin + self.step * index
ap = ArithmeticProgression(0, 1, 3)
list(ap)
[0, 1, 2]
</pre>
其实在Python的itertools,提供了很多生成器函数给我们使用:
例如:enumerate(iterable, start=0),产出由两个元素组成的元组,结构时(index, item),其中index从start开始计数,item则从iterable中获取
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n154">>>> list(enumerate('abcdefg'))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g')]
</pre>
:map(func, it1, [it2,..., itN]), 把it中的各个元素传给func,产出结果;如果传入N个可迭代的对象,那么func必须能接受N个参数,而且要并行处理各个可迭代的对象
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n156">>>> list(map(operator.mul, range(11), range(11)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
</pre>
还有很多很多的方法,详细的可以去官网搜相应的包
装饰器
终于来到装饰器啦,在装饰器这一章会有很多很多的栗子可以吃到,我觉得装饰器的使用能够给我们带来更多的便利,写出的代码也会更加pythonic。
正式讲装饰器之前,我们要先插入闭包这一个概念。
闭包 : 函数内的函数以及其自由变量形成闭包。也即闭包时一个保留定义函数时存在的自由变量的绑定的函数,这样在调用函数时,绑定的自由变量依旧可用!
闭包有什么好处呢?
闭包可以避免全局变量的使用以及提供某种形式数据的隐藏。当函数中的变量和函数较少且其中某个功能常用时,使用闭包来封装。当变量和函数更加复杂时,则使用类来实现。
又来一个栗子吧!
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n167"># 计算移动平均值的函数
def make_averager():
series = []
def averager(new_value):
series.append(new_value)
total = sum(series)
return total/len(series)
return averager
// 在这里边 series = [] 到第七行 return total/len(series)为闭包,变量series为averager()函数中的自由变量!</pre>
来我们进入正文:
让我们一步一步的来深入的(一步一步引导面试官走进来):
1).装饰器的基础知识
我们假设又一个decorate的装饰器
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: normal; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n173">@decorate
def target():
print('running target()!')
// 其实这个代码相当与以下的效果
target = decorate(target)
</pre>
装饰器有两大特性:
第一大特性是, 能把被装饰的函数替换成其他函数。
<mark style="box-sizing: border-box; font-variant-ligatures: none; background: rgb(255, 255, 0); color: rgb(0, 0, 0);">第二大特征是,装饰器在加载模块时立即执行(一定要记得,面试的时候可能会被问到)</mark>
后面会有很多很多的实例!
<pre class="md-fences mock-cm md-end-block" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: pre-wrap; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n181">// 通用模式
def mydecorator(func):
def wrapped(*args, *kwargs):
# 在函数调用前作点什么
result = func(args, **kwargs)
# 在函数调用后,做点什么
# 返回结果
return result
return wrapped
</pre>
我们来实现一个计算函数实现时间的函数吧!
<pre class="md-fences mock-cm md-end-block" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: pre-wrap; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n183">import time
def clock(func):
def clocked(*args, *kwargs):
t1 = time.time()
result = func(args, **kwargs)
t2 = time.time()
elapsed = t2 - t1
print("This func cost: {}seconds".format(elapsed))
return result
return clocked</pre>
通过上面的栗子,我们应该知道了装饰器的工作原理,但还不远远不止如此,我们继续深入下去。
带有参数的函数装饰器
装饰器本身时一个函数,做为一个函数,如果不能传参,那这个函数的功能就会很受限,只能执行固定的逻辑。这意味着,如果装饰器的逻辑代码的执行需要根据不同场景进行调整。如果不能传参的话,这显然不合理。
<pre class="md-fences mock-cm md-end-block" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: pre-wrap; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n188">def repeat(number=3):
"""多次重复执行装饰函数"""
def actual_decorator(func):
def wrapper(*args, *kwargs):
result = None
for _ in range(number):
result = func(args, **kwargs)
return result
return wrapper
return actual_decorator</pre>
我们看到别人的一些装饰器中还会有wraps()这个函数装饰,那这个函数时怎么用的呢?
如果我们想上面那样实现装饰器,装饰器会覆盖了被装饰器函数的*name和*doc__属性。如果我们使用functools.wraps装饰器就把相关的属性从func中复制到clocked中。
如下:
<pre class="md-fences mock-cm md-end-block" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: pre-wrap; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n192">import time
import functools
def clock(func):
@functools.wraps(func)
def clocked(*args, *kwargs):
t1 = time.time()
result = func(args, **kwargs)
t2 = time.time()
elapsed = t2 - t1
print("This func cost: {}seconds".format(elapsed))
return result
return clocked</pre>
你以为这就完了嘛?太天真了,我们再来点高阶的
基于类实现的装饰器
基于类装饰器的实现,必须实现call和init两个内置的函数。
1.init:接受被装饰函数。
2.call:实现装饰逻辑。
下面以一个日志打印为例
<pre class="md-fences mock-cm md-end-block" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: pre-wrap; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n200">class logger(object):
def init(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print("[INFO]: the function {func}() is running...".format(func=self.func.__name__))
return self.func(*args, **kwargs)
@logger
def say(something):
print("say {}!".format(something))</pre>
带参数的类装饰器
!!!带参数和不带参数的类装饰器有很大的不同。
1)init: 不再接受被装饰函数,而是接受传入参数。
2)call:接受被装饰函数,实现装饰逻辑
<pre class="md-fences mock-cm md-end-block" lang="python" style='box-sizing: border-box; font-variant-ligatures: none; overflow: visible; font-family: "JetBrains Mono"; break-inside: avoid; overflow-wrap: break-word; font-size: 0.9em; display: block; text-align: left; white-space: pre-wrap; background: rgb(238, 238, 238); position: relative !important; border-radius: 3px; color: rgb(85, 85, 85); padding: 0.4em 1em; margin: 0px 0px 20px; line-height: 1.5em; width: inherit; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' spellcheck="false" mdtype="fences" cid="n206">class logger(object):
def init(self, level='INFO'):
self.level = level
def __call__(self, func):
def wrapper(*args, **kwargs):
print("[{level}]: the function {func}() is running...".format(level=self.level, func=self.func.__name__))
func(*args, **kwargs)
return wrapper
@loggerK(level='WARNING')
def say(something):
print("say {}!".format(something))</pre>
哈喽,我是海森堡,如果觉得文章对你有帮助,欢迎分享给你的朋友,也给我点个<mark style="box-sizing: border-box; font-variant-ligatures: none; background: rgb(255, 255, 0); color: rgb(0, 0, 0);">在看</mark>,这对我非常重要,给各位哥哥姐姐们抱拳,我们下次见