Python.变量提示 [英] Python. Hint for variable
问题描述
我使用 PyCharm
作为编辑器.例如,我有下一个功能:
def get_instance():# 某些模块中没有文档字符串的方法# 返回 MyClass 的实例返回 some_var
我有第二个文件调用 get_instance()
:
var = get_instance()
如何定义值的数据类型?我想对变量使用自动完成功能.
例如在 php
中它会是这样的:
/** @var MyClass $var */$var = getInstance();
在此之后,我将看到 $var
的所有方法和属性.如何为python变量声明文档字符串?我知道 :type
和 :rtype
,但是如果我需要为特定变量声明类型?我可以声明几种类型吗?我该怎么办?
注意:我无法对 get_instance()
进行任何更改.
您可以按照与类的描述属性相同的方式进行.这里的例子:
from example_module import MyClassvar = get_instance()""":type var: MyClass"""
你也可以像这样使用没有导入的描述:
var = get_instance()""":type var: example_module.MyClass"""
第二种方法是使用
I'm using PyCharm
as editor. For example, I have next function:
def get_instance():
# method without doc sctring in some module
# returns instance of MyClass
return some_var
And I have second file which calls get_instance()
:
var = get_instance()
How I can to define type of data for value? I want to use autocomplete for variable.
For example in php
it will be like this:
/** @var MyClass $var */
$var = getInstance();
After this I will be see all methods and properties of $var
. How to declare docstring for python variable? I know about :type
and :rtype
, but if I need declare type for specific variable? Can I declare a few types? What I should do?
Note: I can't do any changes with get_instance()
.
You can do it in the same way as description properties of class. Here example:
from example_module import MyClass
var = get_instance()
"""
:type var: MyClass
"""
Also you can use description without import like this:
var = get_instance()
"""
:type var: example_module.MyClass
"""
The second way is using PEP 484:
test = my() # type: dict
Examples of autocomplete in Pycharm:
这篇关于Python.变量提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!