自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用這個 Python 3.7 的特性來切片無限生成器

開發(fā) 后端
了解更多關(guān)于這個和其他兩個未被充分利用但仍然有用的 Python 特性。這是關(guān)于 Python 3.x 首發(fā)特性系列文章的第八篇。Python 3.7 于 2018 年首次發(fā)布,盡管它已經(jīng)發(fā)布了幾年,但它引入的許多特性都未被充分利用,而且相當酷。下面是其中的三個。

了解更多關(guān)于這個和其他兩個未被充分利用但仍然有用的 Python 特性。

這是關(guān)于 Python 3.x 首發(fā)特性系列文章的第八篇。Python 3.7 于 2018 年首次發(fā)布,盡管它已經(jīng)發(fā)布了幾年,但它引入的許多特性都未被充分利用,而且相當酷。下面是其中的三個。

注解推遲評估

在 Python 3.7 中,只要激活了正確的 __future__ 標志,注解在運行時就不會被評估:

  1. from __future__ import annotations
  2.  
  3. def another_brick(wall: List[Brick], brick: Brick) -> Education:
  4. pass
  1. another_brick.__annotations__
  1. {'wall': 'List[Brick]', 'brick': 'Brick', 'return': 'Education'}

它使遞歸類型(指向自己的類)和其他有趣的事情成為了可能。然而,這意味著如果你想做自己的類型分析,你需要明確地使用 ast。

  1. import ast
  2. raw_type = another_brick.__annotations__['wall']
  3. [parsed_type] = ast.parse(raw_type).body
  1. subscript = parsed_type.value
  2. f"{subscript.value.id}[{subscript.slice.id}]"
  1.     'List[Brick]'

itertools.islice 支持 index

Python 中的序列切片長期以來一直接受各種 類 int 對象(具有 __index__() 的對象)作為有效的切片部分。然而,直到 Python 3.7,itertools.islice,即核心 Python 中對無限生成器進行切片的唯一方法,才獲得了這種支持。

例如,現(xiàn)在可以用 numpy.short 大小的整數(shù)來切片無限生成器:

  1. import numpy
  2. short_1 = numpy.short(1)
  3. short_3 = numpy.short(3)
  4. short_1, type(short_1)
  1.     (1, numpy.int16)
  1. import itertools
  2. list(itertools.islice(itertools.count(), short_1, short_3))
  1.     [1, 2]

functools.singledispatch() 注解注冊

如果你認為 singledispatch 已經(jīng)很酷了,你錯了?,F(xiàn)在可以根據(jù)注解來注冊了:

  1. import attr
  2. import math
  3. from functools import singledispatch
  4.  
  5. @attr.s(auto_attribs=True, frozen=True)
  6. class Circle:
  7.     radius: float
  8.        
  9. @attr.s(auto_attribs=True, frozen=True)
  10. class Square:
  11.     side: float
  12.  
  13. @singledispatch
  14. def get_area(shape):
  15.     raise NotImplementedError("cannot calculate area for unknown shape",
  16.                               shape)
  17.  
  18. @get_area.register
  19. def _get_area_square(shape: Square):
  20.     return shape.side ** 2
  21.  
  22. @get_area.register
  23. def _get_area_circle(shape: Circle):
  24.     return math.pi * (shape.radius ** 2)
  25.  
  26. get_area(Circle(1)), get_area(Square(1))
  1.     (3.141592653589793, 1)

歡迎來到 2017 年

Python 3.7 大約是四年前發(fā)布的,但是在這個版本中首次出現(xiàn)的一些特性非常酷,而且沒有得到充分利用。如果你還沒使用,那么將它們添加到你的工具箱中。 

 

責任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2017-09-06 09:26:03

Python生成器協(xié)程

2023-07-21 17:08:30

2021-12-04 22:07:44

Python

2020-04-30 21:40:14

C#特性編程語言

2022-10-27 13:58:32

Python編程生成器

2010-09-07 16:31:17

SQL語句insert

2021-04-22 21:15:38

Generator函數(shù)生成器

2011-12-23 13:42:05

JavaScript

2015-08-25 11:07:58

2017-07-01 16:02:39

分布式ID生成器

2025-01-23 08:36:27

CSS開發(fā)工具

2021-08-23 07:32:57

生成器開發(fā)代碼

2017-06-26 16:26:15

Python迭代對象迭代器

2022-10-17 18:29:55

2020-10-05 21:57:43

Python生成器可迭代對象

2023-05-04 16:24:10

人工智能圖像生成器

2023-02-07 16:11:41

2022-07-25 10:27:36

背景生成器工具前端

2024-11-01 15:51:06

2023-11-15 13:35:00

迭代器生成器Python
點贊
收藏

51CTO技術(shù)棧公眾號