0

How can I document a variable in Python. In JavaScript / JSDoc I can do something like that.:

/** @type {Array<Number>} */
var foo;
/** @type {Number[]} */
var bar;

Some IDEs than can give better code completion.

Is this also possible in Python?

5
  • 2
    python.org/dev/peps/pep-0484/#type-comments ... for questions like this first google "pep8" and your question... you will find the reference in the python style guide Commented Sep 30, 2016 at 22:47
  • its probably important to note that for the most part the type is unenforced... and of coarse whether you get any typehinting or help depends on the IDE you are using Commented Sep 30, 2016 at 22:51
  • Thank you, it seems to work, but I have to write # type: list[int] instead of # type: List[int]. Is that correct? It also seems that PyCharm supports it. Commented Sep 30, 2016 at 22:52
  • x, y, z = [], [], [] # type: (List[int], List[int], List[str]) is how it is defined in the spec ... your particular IDE may not implement it in this way ... Commented Sep 30, 2016 at 22:54
  • Ah okay, please answer and I will accept it. Commented Sep 30, 2016 at 22:55

1 Answer 1

1

x, y, z = [], [], [] # type: (List[int], List[int], List[str]) is how it is defined in the spec ... your particular IDE may or may not implement it in this way ...

see also: https://www.python.org/dev/peps/pep-0484/#type-comments

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.