Skip to content

Type Hints - A Function's Instruction Manual ​

You can write types next to variables or parameters, like id: str or amount: int. (The Transaction in the dataclass article also uses this notation.)

Type hints = marking that "this function takes this type and returns this type."

python
def get_month_summary(month: str) -> dict:
    ...

def add_transaction(amount: int, category: str) -> Transaction:
    ...
  • month: str = month must be text, -> dict = the result is a dictionary
  • They are only weakly enforced at runtime, but they catch wrong usage early (editor warnings) and readers understand them right away
  • This is an "input/output contract": the promise functions make to each other, written explicitly in code

Summary ​

ConceptSummary
Type hintsAn instruction manual/promise for a function's inputs and outputs

Source: Codyssey-B1/B2-1