Python example: type hints
Last modified: March 18, 2021
xxxxxxxxxx
# primitives
def func(a: int, b: float) -> str:
a: str = f"{a}, {b}"
return a
# complex types and collections
from typing import List
class Person:
name: str
age: int
def get_names(people: List[Person]) -> List[str]:
return [person.name for person in people]
# map/dict
from typing import Dict
def get_counts(words: List[str]) -> Dict[str, int]:
counts = {}
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
Result
Console output