Meaning
Declares an empty list variable with a static type hint, indicating the list will contain elements of a specific type. Use when you want type‑checked collections and clear intent for readers and tools.
Primary Function
Variable declaration with type hinting
Communicative Purpose
Express that a variable holds a homogeneous collection and that it starts empty
Pattern
identifier: List[element_type] = []
Core Structure
...: List[...] = []
Função primária
Variable declaration with type hinting
Propósito comunicativo
Express that a variable holds a homogeneous collection and that it starts empty
Situações de gatilho
General Python: initializing a list to collect integers later; Class definition: defining a container attribute in a class; Function return: returning an empty typed list from a function
Contextos
General Python codebases, data‑processing scripts, classes, functions, any project using static typing (PEP 484)
Padrão
identifier: List[element_type] = []
Estrutura central
...: List[...] = []
Slots de substituição
identifier: variable name (identifier); element_type: type of items stored in the list (type expression)
Colocados típicos
- append
- extend
- list comprehension
- typing import List
Substituições comuns
- numbers = [] # without type hint
- numbers: list[int] = [] # Python 3.9+ syntax
Erros comuns
Forgetting "from typing import List" → NameError; using mutable default argument in function definitions → shared mutable default across calls; mismatching the hinted type and actual elements added → static type checker warnings
Similar / contraste
Tuple[int, ...] = () – immutable sequence; numbers: List[int] = list() – same effect but more verbose; using [] without a hint provides no static type information
Interferências
Coming from dynamically‑typed languages: may think the hint enforces runtime checks → it only informs static analysis tools
Família do chunk
- type hinting
- collection initialization
- variable declaration
Nuance
Do not use when the list needs to hold heterogeneous types; negligible runtime overhead as hints are erased at runtime; be aware that the hint does not prevent adding incompatible types at runtime, only static checkers flag it
Efeito pragmático
Makes intent explicit, enables static type checking, prevents accidental insertion of wrong‑type items
Dica de memória
Typed empty list – think of a blank typed container ready to be filled
Nota
Import List from the typing module (or use built‑in list[int] on Python 3.9+) before the declaration
Upgrade path
Use a more specialized collection, e.g., numbers: Deque[int] = deque() for efficient front‑insertions
Log in to save chunks.