什么是“全局语句中列出的名称不能在。。。Python3.8文档中的“变量注释”是什么意思?

2024-05-13 21:11:51 发布

您现在位置:Python中文网/ 问答频道 /正文

Python3.8文档(https://docs.python.org/3.8/reference/simple_stmts.html#the-global-statement)说:

Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, import statement, or variable annotation.

“在变量注释中”是什么意思?例如,当我尝试在Python3.7中的全局注释上使用类型注释时,我得到一个错误:

global x
x: int

给出SyntaxError: annotated name 'x' can't be global

然而,根据https://bugs.python.org/issue34939的说法,这种行为是一个bug,在3.8中执行良好时,它似乎已经被修复

那么这条线限制了什么呢


Tags: orthein文档httpsorgdocshtml