为什么类型提示不能在for循环中使用

问题描述 投票:-1回答:1

for i: str in test_string:

我收到了无效语法错误,我想知道背后的原因。

python python-3.x
1个回答
1
投票

您需要根据https://www.python.org/dev/peps/pep-0526/#where-annotations-aren-t-allowed在循环之前启动它>

所以您可以这样做

i : str
for i in test_string: 
    ......
    ......
© www.soinside.com 2019 - 2024. All rights reserved.