V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-11-20 14:38:19 +08:00 · 1896 次点击
    这是一个创建于 1259 天前的主题,其中的信息可能已经有所发展或是发生改变。
    手册中:
    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 条回复    2020-11-20 17:25:01 +08:00
    aijam
        1
    aijam  
       2020-11-20 15:26:25 +08:00   ❤️ 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  
       2020-11-20 15:28:16 +08:00   ❤️ 1
    我判断类型从来不用 typing 的模块,只用 collections.abc 。
    写类型注解的时候才用 typing
    xiaolinjia
        3
    xiaolinjia  
       2020-11-20 15:43:03 +08:00
    py36,刚试了下,collections.abc 的 Sequence 等容器不能在类型注解的时候写成 Sequence[str]啊。只有 typing 下的 Sequence 才可以写成 Sequence[str]。
    imn1
        4
    imn1  
    OP
       2020-11-20 15:44:16 +08:00
    @aijam
    明白了
    collections.abc.Iterable now supports [] ➡️ collections.abc.Iterable 开始支持子类型
    刚才还没想明白这个方括号指什么……
    imn1
        5
    imn1  
    OP
       2020-11-20 15:49:02 +08:00
    @xiaolinjia #3
    以粗陋的英语能力理解,是需要带__class_getitem__()魔术方法,而这个是 py3.9 加入的
    aijam
        6
    aijam  
       2020-11-20 15:57:11 +08:00
    @imn1 这玩意儿不叫子类型,叫 parameterized generics
    imn1
        7
    imn1  
    OP
       2020-11-20 16:00:20 +08:00
    @aijam #6
    对,我只是词穷,随手写,🐶
    xiaolinjia
        8
    xiaolinjia  
       2020-11-20 16:09:26 +08:00
    @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
       2020-11-20 16:24:34 +08:00
    @xiaolinjia #8
    我反正感觉 py39 要改一波代码,现在没写 type hint 还好说……

    另外想写个类型相关的装饰器,用来临时检查也好,实用类型转换也好,如果 typing 相关在 3.9 弃用,就要重新考虑怎么写了,今天主要想搞清这个问题
    xiaolinjia
        10
    xiaolinjia  
       2020-11-20 17:25:01 +08:00
    @imn1 刚咨询了下大佬,从 37 引入__class_getitem__方法开始,就可以通过 from__future__ import annotations 来使用 a: list[str] = ['a']了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2256 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:26 · PVG 15:26 · LAX 00:26 · JFK 03:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.