WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / Python

typing.Callable

Beschreibt ein aufrufbares Objekt durch Parameter- und Ergebnistypen.

Version: Python 3.14; repository image, production runtime not attested

Syntax

Callable[[int, int], str]

Beschreibt ein aufrufbares Objekt durch Parameter- und Ergebnistypen.

Eine Liste nennt Positionsargumente, Auslassungspunkte lassen die Parameterliste offen. In Python 3.14 ist typing.Callable ein veralteter Alias von collections.abc.Callable.

Ein bloßer Aufrufbarkeitstest prüft die Argumentanzahl nicht.

Beispiele

from typing import Callable
from collections.abc import Callable as CallableABC

def run(fn: Callable[[int, int], str]) -> str:
    return fn(2, 4)

print(run(lambda a, b: str(a + b)))
bad = lambda: "empty"
print(isinstance(bad, CallableABC))
try:
    run(bad)
except TypeError:
    print("wrong arity")

Ergebnis

6
True
wrong arity

Ein bloßer Aufrufbarkeitstest prüft die Argumentanzahl nicht.

Quellen