gpt4 book ai didi

python - 常用文档字符串列表 :types for pycharm

转载 作者:太空狗 更新时间:2023-10-30 01:48:01 24 4
gpt4 key购买 nike

Pycharm 有一个很好的功能,它可以读取函数文档字符串中的类型声明,并在使用该函数的地方进行正确使用检查。

是否有很好的资源列出所有常用类型的规则和名称?

注意:
我在下面提供了一个答案,其中包含我能够弄清楚的所有内容。不过,它远非全面。

最佳答案

这是我常用的类型列表:

string:             str
unicode: unicode
integer: int
float: float
general dictionary: dict
general list: list
general tuple: tuple
None: None
not defined: Any
your own object: MySpecialClass

specific tuple: (str, int)
specific list: list of str
specific dict: dict[str, int]
multiple options: str or list

例子:

import requests
def example_function(list_var, str_var, num_var, request_var):
"""One line function explanation.

:param list_var:
:type list_var: list of str
:param str_var:
:type str_var: str or unicode
:param num_var:
:type num_var: int
:param request_var:
:type request_var: requests.models.Request
:return:
:rtype: (list of dict) or None
"""
return [{}]


example_function(
['a', 'b'],
u'unicode accepted because stated',
1.234,
requests.Request('GET', 'http://going/somewhere')
)

通过跟踪 __init__.py 中的模块和类找到请求的正确格式

然后,当按下 Ctrl 并将鼠标悬停在函数调用上时,会得到一个类型帮助文本。这非常有用,因为 PEP 8命名约定在设计上是类型模糊的。

在这种情况下,帮助文本将是:

Inferred type: (list_var: List[str], str_var: Union[str, unicode], num_var: int, request_var: Request) -> Optional[List[dict]]

还有一个针对 num_var 参数的警告:

Expected type 'int', got 'float'

关于python - 常用文档字符串列表 :types for pycharm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40149689/

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