Very recently I have seen something like this in Python:
const: str = 5;
I thought it would give an error, since one is assigning a numerical value to a string declared constant; however, when I print const+5, hopping for another error, it gives me 10, as if const was a numerical constant.
What’s happening here?
>Solution :
There are two things to understand here:
consthas no special meaning in Python. Here it’s just being used as a variable name (which is somewhat evil, since it can only be intended to confuse the unwary).- Type annotations are not enforced by the Python interpreter. Assigning an incompatible type will, however, produce an error if you run a static typechecker on your code:
test.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
Also, note that semicolons are not required in Python, and are generally considered bad style.