Changing types when reading a number


A quite common case in the real-world when you read in something that is supposed to be a number. In terms of the Python type-system the input is always a string. Even if it looks like a number. We then need to convert it to int() or to float() to use them as such.

People will often reuse the same variable to first hold the string and then the number. This is ok with Python, but might be confusingt to the reader.


examples/types/input.py
num = input("type in an integer: ")
print(num)
print(type(num).__name__)   # str

num = int(num)
print(num)
print(type(num).__name__)   # int

mypy input.py will print the following:


input.py:6: error: Incompatible types in assignment (expression has type "int", variable has type "str")
Found 1 error in 1 file (checked 1 source file)