Danila Ganchar Danila Ganchar - 21 days ago 5
Python Question

Python. Hint for variable

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()
.

Answer

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
"""
Comments