新聞中心
了解更多關(guān)于這個和其他兩個未被充分利用但仍然有用的 Python 特性。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比大理州網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式大理州網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋大理州地區(qū)。費用合理售后完善,10年實體公司更值得信賴。
這是關(guān)于 Python 3.x 首發(fā)特性系列文章的第八篇。Python 3.7 于 2018 年首次發(fā)布,盡管它已經(jīng)發(fā)布了幾年,但它引入的許多特性都未被充分利用,而且相當(dāng)酷。下面是其中的三個。
注解推遲評估
在 Python 3.7 中,只要激活了正確的 __future__ 標志,注解在運行時就不會被評估:
from __future__ import annotationsdef another_brick(wall: List[Brick], brick: Brick) -> Education:pass
another_brick.__annotations__
{'wall': 'List[Brick]', 'brick': 'Brick', 'return': 'Education'}
它使遞歸類型(指向自己的類)和其他有趣的事情成為了可能。然而,這意味著如果你想做自己的類型分析,你需要明確地使用 ast。
import astraw_type = another_brick.__annotations__['wall'][parsed_type] = ast.parse(raw_type).body
subscript = parsed_type.valuef"{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 numpyshort_1 = numpy.short(1)short_3 = numpy.short(3)short_1, type(short_1)
(1, numpy.int16)
import itertoolslist(itertools.islice(itertools.count(), short_1, short_3))
[1, 2]
functools.singledispatch() 注解注冊
如果你認為 singledispatch 已經(jīng)很酷了,你錯了?,F(xiàn)在可以根據(jù)注解來注冊了:
import attrimport mathfrom 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@singledispatchdef get_area(shape):raise NotImplementedError("cannot calculate area for unknown shape",shape)@get_area.registerdef _get_area_square(shape: Square):return shape.side ** 2@get_area.registerdef _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)的一些特性非???,而且沒有得到充分利用。如果你還沒使用,那么將它們添加到你的工具箱中。
本文標題:用這個Python3.7的特性來切片無限生成器
網(wǎng)頁地址:http://fisionsoft.com.cn/article/dhisdci.html


咨詢
建站咨詢

