基于类型的PyCharm中的自动补全 [英] Autocompletion in PyCharm based on type

查看:280
本文介绍了基于类型的PyCharm中的自动补全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyCharm进行python编码. PyCharm中的自动完成功能不如IntelliJ(Java)中的自动完成功能.考虑下面的代码

I'm using PyCharm for python coding. The autocompletion in PyCharm is not as good as in IntelliJ (Java). Consider the below code

a = [1,2,3,4]
a.

在这种情况下,按dot后,PyCharm会提供完整的自动完成选项集.考虑以下情况

In this case, after I press the dot, PyCharm gives the full set of autocompletion options. Consider the below case

def func_a(a):
    a.

在这里,我将list作为参数传递给函数func_a.但是当我在a之后按dot时,PyCharm没有提供任何自动完成选项.我知道这是因为Python是动态类型化的语言,而PyCharm不知道确定a是什么类型.

Here, to the function func_a I'm passing a list as an argument. But when I press dot after a, PyCharm doesn't give any autocompletion options. I know this is because Python is dynamically typed language and PyCharm has no idea to determine what type a is.

但是有什么方法可以告诉PyCharm a的类型,可能在文档注释中或类似的内容吗?那么,PyCharm可以提供有效的自动完成选项吗?

But is there any way to tell PyCharm the type of a, may be in documentation comment or something like that? So, that PyCharm can give valid autocompletion options?

推荐答案

是的,Python 3.5引入了类型提示,而Pycharm则利用了这种类型.

Yes, Python 3.5 introduces type hinting and Pycharm utilizes that.

使用

def func_name(param: type) -> return_type:

喜欢

def func_a(a: list):

请注意,所有类型提示都是完全可选的,并且被解释器忽略.但是,如果将Pycharm用作习惯,则可以潜在地帮助您检测类型错误.

Note that all type hints are completely optional and ignored by the interpreter. However, Pycharm can potentially help you detect type errors if you use it as a habit.

这篇关于基于类型的PyCharm中的自动补全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆