gpt4 book ai didi

Python 不允许在解包时注释变量的类型

转载 作者:行者123 更新时间:2023-12-04 19:25:17 25 4
gpt4 key购买 nike

PEP 526说如下:

Note that, although the syntax does allow tuple packing, it does not allow one to annotate the types of variables when tuple unpacking is used.



为什么python在解包时不支持变量的类型注释?
附注。我必须先对类型进行注释,然后再进行解包,这很好,只是在解包时进行注释会更整洁。

最佳答案

Rejected/Postponed Proposals section 中有一些关于此的信息。 PEP 526 的:

Allow type annotations for tuple unpacking: This causes ambiguity: it's not clear what this statement means:

x, y: T

Are x and y both of type T, or do we expect T to be a tuple type of two items that are distributed over x and y, or perhaps x has type Any and y has type T? (The latter is what this would mean if this occurred in a function signature.) Rather than leave the (human) reader guessing, we forbid this, at least for now.


该评论似乎表明,最终可能会提出一种更简单的语法,并且不容易被误解。现在,我们不得不分别注释元组的类型。

关于Python 不允许在解包时注释变量的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59146752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com