0

I'm using this feature of python and thinking is that ok to specify the type of a variable which the type is obvious?

For instance, I have this variable.

my_list: list = [1, 2, 3]

Definitely, this variable is list but my question is:

Is this a good approach to specify all these variables types?

sophros
  • 14,672
  • 11
  • 46
  • 75
Omid Zarinmahd
  • 158
  • 1
  • 1
  • 11

1 Answers1

1

Since type hinting is an optional feature of the language there will be varying opinions on the topic. It is of course allowed but (the vast majority of times) unnecessary and definitely not pythonic in the sense of succinctness.

Starting with Python version 3.5 (if I am not mistaken) you can help your memory, your IDE or style checker and add optional types to your code. Since the power of Python is in its dynamic nature typically it is a feature of the language that you should use sparingly unless you have a good reason to do so (as above, or for documentation purposes, to find bugs easier).

Type hints can can be seen as verbiage that clutters the code and it definitely adds to reading burden.

There is a lengthy discussion in this answer which you may find useful to shed the light on the topic.

sophros
  • 14,672
  • 11
  • 46
  • 75