as — Expert Examples
Creates an alias (used with import, with, except)
as bytecode across contexts
How 'as' compiles in different uses.
python
import dis def import_as(): import json as j return j def except_as(): try: pass except ValueError as e: return e def with_as(): class CM: def __enter__(self): return 1 def __exit__(self, *a): pass with CM() as val: return val print("import as:") dis.dis(import_as) print("\nexcept as:") dis.dis(except_as) print("\nwith as:") dis.dis(with_as) # import as: IMPORT_NAME + STORE_FAST (stores under alias name) # except as: STORE_FAST + DELETE_FAST after block # with as: STORE_FAST (stores __enter__ return value)
'as' is not a single bytecode operation — it's syntactic sugar. Import as renames the stored name. Except as stores then deletes the exception. With as stores the __enter__ return value.
Want to try these examples interactively?
Open Expert Playground