Python has introduced type hinting, which mean we could hinting the type of variable, this was done by doing variable: type (or parameter: type), so for example target is a parameter, of type integer.
the arrow (->) allows us to type hint the return type, which is a list containing integers.
from typing import List
Vector = List[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
In the function greeting, the argument name is expected to be of type str and the return type str. Subtypes are accepted as arguments.
def greeting(name: str) -> str:
return 'Hello ' + name
Documentation : https://docs.python.org/3/library/typing.html
from typing import List
, I still encounter the issue fortarget: int
:TypeError: twoSum() missing 1 required positional argument: 'target'
; any ideas? – Rosales