3 ms·
> Python is a dynamically typed language. By definition, you don’t know the real types of variables at runtime. I think you mean compile-time and not runtime.
by runeks 5y ago
> Python is a dynamically typed language. By definition, you don’t know the real types of variables at runtime.
I think you mean compile-time and not runtime. There’s not much use in knowing the type at runtime, since it only enables you to print a better error before crashing (“Expected type: x, actual type: y” instead of “AttributeError: 'str' object has no attribute 'append'”).
Types are only truly valuable at compile-time, where they can prevent compilation of invalid code. This is the reason many statically typed languages use type erasure,
which removes type information from the compilation output.
In fact, the type-checking phase can happen even earlier than the compilation phase. This is useful e.g. in an IDE, where you care about type errors but not necessarily about running your program.