Skip to content Skip to sidebar Skip to footer

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 som

Solution 1:

I don't think you can do this via docstring, but there is a mechanism called annotation. You can append whatever expression you want to parameters following a colon : and to the function declaration itself using an arrow ->. For example:

defgetInstance(param: str) -> MyClass
    return some_var

I have added an input parameter for purposes of illustration here. In this case str and MyClass are objects that represent the expected input and return types, respectively. They are never used in your code directly, just stashed away in an func_annotations attribute for end users that care about that sort of things (like editors).

Solution 2:

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:

enter image description here

enter image description here

enter image description here

Post a Comment for "Python. Hint For Variable"