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?

1
  • This is a quite good question, but I'm afraid it's OT here. Commented Feb 19, 2019 at 10:43

1 Answer 1

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.

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.