Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword as

Bind a name according to the surrounding statement.

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

Syntax

import module as alias
with manager as value:
    ...

Bind a name according to the surrounding statement.

An import alias refers to the imported object, a with target receives the result of __enter__, and an except target names the exception only during handling. An as pattern captures the whole matched subject after its subpattern succeeds.

These bindings have different lifetimes.

Examples

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())

Result

[2, 4] tag False

These bindings have different lifetimes.

Sources