Functions, Type Hints & Decorators

~15 min read

Typed Python functions with clear signatures are essential for AI codebases. Type hints enable IDE autocomplete and make LLM-assisted coding significantly more effective.

Functions, Type Hints & Decorators is a Pro topic

Sign in, then upgrade to Pro or Power to unlock this topic and the full AI Engineering curriculum.

Key points

  • Type hints: def fn(x: str, n: int = 10) -> list[str]
  • @dataclass for config objects — cleaner than plain dicts
  • @torch.no_grad() during inference — reduces memory by 30–50%
  • Optional[str] means the parameter can be str or None