推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
imn1
V2EX  ›  Python

这句话的翻译是?(PEP585)

  •  
  •   imn1 · Nov 20, 2020 · 2431 views
    This topic created in 2025 days ago, the information mentioned may be changed or developed.
    手册中:
    class typing.Iterable(Generic[T_co])
    A generic version of collections.abc.Iterable.
    Deprecated since version 3.9: collections.abc.Iterable now supports []. See PEP 585.

    后面这句指的是 3.9 弃用 collections.abc.Iterable,还是弃用 typing.Iterable ?
    它用逗号我理解是弃用 typing.Iterable,用冒号我就糊涂了

    PEP585 中有这句:
    This PEP proposes to enable support for the generics syntax in all standard collections currently available in the typing module.

    PEP585 太长,英语不佳

    其实一直对多个模块( types, collections, typing 等)重复的类型定义不解,不知道下面这样理解对不对?
    多个模块对于类型的定义,依据 PEP585 弃用,转到 typing 模块去归一定义

    还是要反向理解,而 typing 模块只着重于处理方法,定义由其他模块完成

    希望是用 typing 归一,判断一个类型要 import 好几个模块真是累
    10 replies    2020-11-20 17:25:01 +08:00
    aijam
        1
    aijam  
       Nov 20, 2020   ❤️ 1
    先回答你的问题:
    > 后面这句指的是 3.9 弃用 collections.abc.Iterable,还是弃用 typing.Iterable?
    弃用 typing.Iterable
    > 转到 typing 模块去归一定义
    不对,是反过来:3.9 之后 typing 里很多和标准库本来就有的重复的东西被 deprecate 了
    ------------------------------------------------------------------------------------
    3.9 之前,你写 list of string 的 type annotation 需要写"var: typing.List[str]"。
    3.9 之后,你直接用 builtin 的类型 list,只需要写"var: list[str]",不需要 import typing 。
    基本改革方向就是:“如果标准库里有这个类型了,就不用在 typing 里专门为了 type annotation 又重复定义一个了”。所以,标准库里本来就有 collections.abc.Iterable,它是 list, tuple, dict 等共同的抽象父类,所以没必要保留 typing.Iterable 了。
    xiaolinjia
        2
    xiaolinjia  
       Nov 20, 2020   ❤️ 1
    我判断类型从来不用 typing 的模块,只用 collections.abc 。
    写类型注解的时候才用 typing
    xiaolinjia
        3
    xiaolinjia  
       Nov 20, 2020
    py36,刚试了下,collections.abc 的 Sequence 等容器不能在类型注解的时候写成 Sequence[str]啊。只有 typing 下的 Sequence 才可以写成 Sequence[str]。
    imn1
        4
    imn1  
    OP
       Nov 20, 2020
    @aijam
    明白了
    collections.abc.Iterable now supports [] ➡️ collections.abc.Iterable 开始支持子类型
    刚才还没想明白这个方括号指什么……
    imn1
        5
    imn1  
    OP
       Nov 20, 2020
    @xiaolinjia #3
    以粗陋的英语能力理解,是需要带__class_getitem__()魔术方法,而这个是 py3.9 加入的
    aijam
        6
    aijam  
       Nov 20, 2020
    @imn1 这玩意儿不叫子类型,叫 parameterized generics
    imn1
        7
    imn1  
    OP
       Nov 20, 2020
    @aijam #6
    对,我只是词穷,随手写,🐶
    xiaolinjia
        8
    xiaolinjia  
       Nov 20, 2020
    @imn1 不知道我的理解对不对。我现在的想法是,py36 的时候,还没有抽象出__class_getitem__()魔术方法,这时,如果在类型注解中使用 collections.abc.Sequence[str]时,ide 会提示 type 类没有定义__getitem__魔术方法,所以注解失败。(因为 collections.abc.Sequence 是 type 类的实例)。
    然后 py37 的时候,出现了__class_getitem__魔术方法这个接口,但是内置的类型比如 list,还是没有定义了__class_getitem__这个方法。直到 py39 的时候,内置类型 list 才定义了这个方法,使得 list[str]也能用于注解了。
    imn1
        9
    imn1  
    OP
       Nov 20, 2020
    @xiaolinjia #8
    我反正感觉 py39 要改一波代码,现在没写 type hint 还好说……

    另外想写个类型相关的装饰器,用来临时检查也好,实用类型转换也好,如果 typing 相关在 3.9 弃用,就要重新考虑怎么写了,今天主要想搞清这个问题
    xiaolinjia
        10
    xiaolinjia  
       Nov 20, 2020
    @imn1 刚咨询了下大佬,从 37 引入__class_getitem__方法开始,就可以通过 from__future__ import annotations 来使用 a: list[str] = ['a']了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2691 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:24 · PVG 19:24 · LAX 04:24 · JFK 07:24
    ♥ Do have faith in what you're doing.