[Python-ideas] PEP draft: context variables

francismb francismb at email.de
Wed Sep 6 16:23:49 EDT 2017


Hi!,

> Core concept
> ''''''''''''
>
> A context-local variable is represented by a single instance of
> ``contextvars.Var``, say ``cvar``. Any code that has access to the
``cvar``
> object can ask for its value with respect to the current context. In the
> high-level API, this value is given by the ``cvar.value`` property::
>
>     cvar = contextvars.Var(default="the default value",
>                            description="example context variable")
>
>     assert cvar.value == "the default value"  # default still applies
>
>     # In code examples, all ``assert`` statements should
>     # succeed according to the proposed semantics.

[...]

> Running code in a clean state
> '''''''''''''''''''''''''''''
>
> Although it is possible to revert all applied context changes
> using the above primitives, a more convenient way to run a block
> of code in a clean context is provided::
>
>     with context_vars.clean_context():
>         # here, all context vars start off with their default values
>     # here, the state is back to what it was before the with block.

why not to call the section 'Running code in the default state' and the
method just `.default_context()`:

with context_vars.default_context():
        # here, all context vars start off with their default values
# here, the state is back to what it was before the with block.

Means `clean` here `default` (the variable is constructed as
cvar = ontextvars.Var(default="the default value", ...) ?

Thanks in advance,
--francis



More information about the Python-ideas mailing list