3 ms·
There is a difference between type _conversion_ and changing the type of a variable. In your example, there's no conversion performed. "a" is dynamically typed,
by hermitdev 3y ago
There is a difference between type _conversion_ and changing the type of a variable. In your example, there's no conversion performed. "a" is dynamically typed, it's type changes with each assignment. The interpreter does not coerce the type at any step.
Your definition of "f" is no different than this in C++:
template<typename T, typename U>
auto f(T x, U y) -> decltype(x + y) { return x + y; }
"f" is duck typed, effectively. It works fine if operator+ is well defined for "x" and "y", otherwise it fails. The difference, though is a compile time error in C++ vs a runtime error in Python (maybe a linter error with appropriate type hints).