用這個 Python 3.7 的特性來切片無限生成器
了解更多關(guān)于這個和其他兩個未被充分利用但仍然有用的 Python 特性。
這是關(guān)于 Python 3.x 首發(fā)特性系列文章的第八篇。Python 3.7 于 2018 年首次發(fā)布,盡管它已經(jīng)發(fā)布了幾年,但它引入的許多特性都未被充分利用,而且相當酷。下面是其中的三個。
注解推遲評估
在 Python 3.7 中,只要激活了正確的 __future__
標志,注解在運行時就不會被評估:
from __future__ import annotations
def another_brick(wall: List[Brick], brick: Brick) -> Education:
pass
another_brick.__annotations__
{'wall': 'List[Brick]', 'brick': 'Brick', 'return': 'Education'}
它使遞歸類型(指向自己的類)和其他有趣的事情成為了可能。然而,這意味著如果你想做自己的類型分析,你需要明確地使用 ast
。
import ast
raw_type = another_brick.__annotations__['wall']
[parsed_type] = ast.parse(raw_type).body
subscript = parsed_type.value
f"{subscript.value.id}[{subscript.slice.id}]"
'List[Brick]'
itertools.islice 支持 index
Python 中的序列切片長期以來一直接受各種 類 int 對象(具有 __index__()
的對象)作為有效的切片部分。然而,直到 Python 3.7,itertools.islice
,即核心 Python 中對無限生成器進行切片的唯一方法,才獲得了這種支持。
例如,現(xiàn)在可以用 numpy.short
大小的整數(shù)來切片無限生成器:
import numpy
short_1 = numpy.short(1)
short_3 = numpy.short(3)
short_1, type(short_1)
(1, numpy.int16)
import itertools
list(itertools.islice(itertools.count(), short_1, short_3))
[1, 2]
functools.singledispatch() 注解注冊
如果你認為 singledispatch 已經(jīng)很酷了,你錯了?,F(xiàn)在可以根據(jù)注解來注冊了:
import attr
import math
from functools import singledispatch
@attr.s(auto_attribs=True, frozen=True)
class Circle:
radius: float
@attr.s(auto_attribs=True, frozen=True)
class Square:
side: float
@singledispatch
def get_area(shape):
raise NotImplementedError("cannot calculate area for unknown shape",
shape)
@get_area.register
def _get_area_square(shape: Square):
return shape.side ** 2
@get_area.register
def _get_area_circle(shape: Circle):
return math.pi * (shape.radius ** 2)
get_area(Circle(1)), get_area(Square(1))
(3.141592653589793, 1)
歡迎來到 2017 年
Python 3.7 大約是四年前發(fā)布的,但是在這個版本中首次出現(xiàn)的一些特性非常酷,而且沒有得到充分利用。如果你還沒使用,那么將它們添加到你的工具箱中。