我想在 for
循环中注释变量的类型。我试过了,但没有用:
for i: int in range(5):
pass
我期望在 PyCharm 2016.3.2 中使用自动完成功能,但使用 预注释不起作用:
i: int
for i in range(5):
pass
附:预注释适用于 PyCharm >= 2017.1。
最佳答案
根据PEP 526 ,这是不允许的:
In addition, one cannot annotate variables used in a
for
orwith
statement; they can be annotated ahead of time, in a similar manner to tuple unpacking
在循环之前注释它:
i: int
for i in range(5):
pass
PyCharm 2018.1 及更高版本 现在可以识别循环内变量的类型。这在旧版 PyCharm 中不受支持。
关于python - 如何在 for 循环中注释类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41641449/