Keywords and Control Flow

as — Alias and target binding

Bind a name or target in a surrounding construct. In imports, as chooses the local name. In with, the target receives the context manager’s enter result, which may be a different object from the manager. In an except clause, the exception target is cleared at the end of the handler. In a structural match AS pattern, the name captures the matched subject when the pattern succeeds. as is not a general assignment operator.

Formal Syntax & Structure

python syntaxpython
import module as aliasfrom module import name as aliaswith manager as target:    suiteexcept Error as err:    suitecase pattern as name:    suite

Working Example

Example Snippetpython
from contextlib import nullcontextimport math as numbers
with nullcontext("ready") as status:    print(status, numbers.isqrt(25))
match (2, 3):    case (x, y) as pair:        print(pair, x + y)
Python Reference