CKTIS / Python
Parola chiave as
Lega un nome secondo l’istruzione.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
import module as alias
with manager as value:
...Lega un nome secondo l’istruzione.
Un alias di import indica l’oggetto importato, il bersaglio di with riceve il risultato di __enter__ e quello di except nomina l’eccezione durante la gestione. Un modello as cattura l’intero soggetto dopo il successo del sottomodello.
I legami hanno durate diverse.
Esempi
import math as numbers
from contextlib import nullcontext
with nullcontext(4) as qty:
value = numbers.isqrt(qty)
try:
raise ValueError("tag")
except ValueError as err:
label = str(err)
match [value, qty]:
case [2, _] as pair:
print(pair, label, "err" in globals())
Risultato
[2, 4] tag False
I legami hanno durate diverse.