5
We know that Python, as of version 3.6, supports annotating types in functions and variables, according to the PEP 526 -- Syntax for Variable Annotations and PEP 3107 -- Function Annotations.
def int2float(x: int) -> float:
return float(x)
a: int = 2
b: float = int2float(a)
However, even after the implementation of these Peps Amblas do not have type annotation support.
int2float = lambda x: int: float(x)
I believe that in this case would break the syntax and generate redundancy because it would not be possible to define whether the lambda is just lambda x: int
, returning to class int
, or if this is the type annotation. However, I believe that adding parentheses, which are currently not supported, would solve the problem:
int2float = lambda (x: int) -> float: float(x)
Apparently they decided that this would be unfeasible or that it would not make sense (except for the fact that the language is dynamic typing).
Why did you choose not to add type annotations in lambda? They didn’t see the need or it doesn’t make sense for a lambda to have type annotations?
Related: Type annotation in a function does not guarantee type in Python 3?
– Woss