def greet(name: str) -> str:
Type System & Annotations

Meaning

This defines a function with explicit type annotations for its parameters and return value. It helps catch type mismatches early and makes the code self‑documenting, addressing the pain of hidden bugs in dynamically typed code. Use it whenever you need a reusable utility where input and output types should be clear.

Primary Function

Function definition

Communicative Purpose

Enables declaring a reusable code block with explicit type safety for arguments and return values.

Pattern

def function_name(parameter_name: parameter_type) -> return_type:

Core Structure

def ...(...) -> ...:

Função primária

Function definition

Propósito comunicativo

Enables declaring a reusable code block with explicit type safety for arguments and return values.

Situações de gatilho

Utility scripts: creating a small helper function Web APIs: defining an endpoint handler that returns a string response Data processing: writing a function to format output values

Contextos

Modern Python 3.5+ codebases, type-checked projects using mypy or pyright.

Padrão

def function_name(parameter_name: parameter_type) -> return_type:

Estrutura central

def ...(...) -> ...:

Slots de substituição

function_name: identifier, parameter_name: identifier, parameter_type: type, return_type: type

Colocados típicos

  • return statement
  • docstrings
  • type imports from typing module

Substituições comuns

  • Omitting type hints for quick scripts
  • using Union or Optional for complex types

Erros comuns

Forgetting the colon, mismatching the actual return type with the hinted type, using mutable default arguments

Similar / contraste

Lambda functions (anonymous, single expression), functions without type hints (dynamic typing)

Interferências

Coming from JavaScript: Python requires 'def' and colons, not 'function' or arrows for standard definitions.

Família do chunk

  • Function definitions
  • lambda expressions
  • type annotations
  • decorators

Nuance

Type hints are not enforced at runtime by default; they require a static checker. Avoid over-using complex generics in simple scripts.

Efeito pragmático

Improves code readability, enables IDE auto-completion, and catches type errors before execution.

Dica de memória

The 'def' keyword starts the definition, colon ends the header, arrow points to the return type.

Nota

Type hints are optional but improve tooling support; they are not enforced at runtime without a static checker.

Upgrade path

Using *args, **kwargs, or Callable types for higher-order functions.

Frequência: Very highFormulaicidade: Semi-fixedTipo de construção: function definitionPrioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.