【发布时间】:2014-09-11 07:10:15
【问题描述】:
使用 Python 3 的函数注释,可以指定同构列表(或其他集合)中包含的项目的类型,以便在 PyCharm 和其他 IDE 中进行类型提示?
一个int列表的伪python代码示例:
def my_func(l:list<int>):
pass
我知道可以使用 Docstring...
def my_func(l):
"""
:type l: list[int]
"""
pass
...但如果可能的话,我更喜欢注释风格。
【问题讨论】:
-
你试过在函数注解中使用相同的格式吗?发生了什么?
-
@jonrsharpe 它应该会引发错误,因为
type object is not subscriptable在定义函数时。显然你可以使用一个字符串:def my_func(L: 'list[int]')但我不知道 PyCharm 在解析文档字符串时是否会解析它... -
@Bakuriu 是的,我的意思是
'list[int]',如果不清楚,请道歉。 -
看起来 PyCharm 不会像解析文档字符串那样解析它。
标签: python python-3.x type-hinting python-typing