[Python-Dev] PEP 343 update (with statement context terminology)

Jim Jewett jimjjewett at gmail.com
Tue Apr 25 22:51:55 CEST 2006


> So things like decimal.Context get left trying to find a sane
> name for what their __context__ method returns.
> decimal.Context.__context__() returns a . . . context? What?
> Wasn't it already a context? Oh, so it actually returns a
> "with statement context object".

[I was OK with "context specifiers", but here is another attempt.]

-----------

With statements execute in a temporary context.

with with_expression as temporary_context_name:
    stmt1
    stmt2


The with_expression must return an object implementing the Temporary
Context protocol.  The Temporary Context's __enter__ method runs
before the statement block, and its __exit__ method runs after the
block.

Temporary Contexts can represent arbitrary changes to the execution
environment.  Often, they just ensure that a file is closed in a
timely fashion.

A more advanced example is Decimal.Context, which controls the
precision of certain math calculations.  Decimal.Context also
implements the Temporary Context protocol, so that you can increase
the precision of a few calculations without slowing down the rest of
the program.

-jJ


More information about the Python-Dev mailing list