Meaning
Specifies that an object is a callable which accepts an integer and a string and returns a Boolean value.
Primary Function
To annotate the expected signature of a callable (function, method, or callable object) for static type checking and documentation.
Communicative Purpose
Communicates the exact input and output types expected from a callable to type checkers and human readers.
Pattern
Callable[[<arg1_type>, <arg2_type>], <return_type>] (specific: Callable[[int, str], bool])
Core Structure
Callable[[<Arg1>, <Arg2>], <Return>] where Arg1=int, Arg2=str, Return=bool.
Função primária
To annotate the expected signature of a callable (function, method, or callable object) for static type checking and documentation.
Propósito comunicativo
Communicates the exact input and output types expected from a callable to type checkers and human readers.
Situações de gatilho
When declaring a parameter, variable, or return type that must be a callable accepting an int and a str and returning a bool, such as event handlers, callbacks, or factory functions.
Contextos
Used in Python type annotations, typically imported from typing (e.g., from typing import Callable) and placed in function signatures, variable annotations, or return type annotations.
Padrão
Callable[[<arg1_type>, <arg2_type>], <return_type>] (specific: Callable[[int, str], bool])
Estrutura central
Callable[[<Arg1>, <Arg2>], <Return>] where Arg1=int, Arg2=str, Return=bool.
Slots de substituição
arg1: type (int), arg2: type (str), return: type (bool)
Colocados típicos
- typing.Callable
- callback functions
- event handlers
- key functions for sorting
- factory functions
- Protocol definitions
Substituições comuns
- Callable[[int]
- bool] – single argument
- less specific
- Callable[[str]
- bool] – different argument type
- Callable[[]
- bool] – no arguments
- used for predicates
- Callable[[int
- str
- float]
- bool] – extra argument
- Callable[[int
- str]
- int] – different return type
- using ParamSpec for generic callables – more flexible but more complex.
Erros comuns
1. Forgetting to import Callable from typing – causes NameError; 2. Omitting the return type (Callable[[int, str]]) – results in an overly generic Callable, losing return‑type safety; 3. Swapping argument order (Callable[[str, int], bool]) – leads to type‑checker flagging mismatched arguments; 4. Using the built‑in callable() instead of typing.Callable – loses ability to specify argument/return types; 5. Misplacing brackets (Callable[[int, str]] -> bool) – syntax error.
Similar / contraste
Callable[[int], bool] – callable with a single int argument; Callable[[], bool] – callable with no arguments; Protocol with __call__ method – defines a callable interface via structural subtyping; plain Callable – unspecified signature, less precise.
Interferências
Coming from Java: may treat Callable like Java’s FunctionalInterface and forget to import typing.Callable, causing NameError → import typing.Callable and specify argument types; Coming from JavaScript: may expect dynamic callables and omit type annotations, resulting in weaker static checking → add explicit Callable[[int, str], bool] annotations for better tooling.
Família do chunk
- type annotations
- callable types
- typing module
Nuance
Do not use when the callable’s signature is unknown or varies dynamically, as over‑specifying can cause false positives; runtime impact is negligible because type hints are erased, though static analysis may incur minor overhead; a callable matching the signature via __call__ on an instance is compatible, whereas a C‑extension function with the same signature may fail certain isinstance checks.
Efeito pragmático
Enables static type checkers to catch mismatched callbacks early, improves IDE autocomplete and refactoring safety, and documents expected callback contracts for maintainability.
Dica de memória
Think of a Callable type hint as a ticket booth that only lets through passengers holding a ticket with an integer ID and a string name, guaranteeing they’ll either be admitted (True) or turned away (False).
Nota
From Python 3.9+ collections.abc.Callable can be used; from Python 3.10 the built‑in collection types support subscripting but Callable still requires typing or collections.abc.
Upgrade path
Learn to use ParamSpec and Concatenate for generic higher‑order functions that preserve callable signatures.
Log in to save chunks.