[Python-ideas] PEP draft: context variables
Pavol Lisy
pavol.lisy at gmail.com
Tue Sep 5 03:43:06 EDT 2017
On 9/4/17, Koos Zevenhoven <k7hoven at gmail.com> wrote:
> 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.
>
>
> No assignments to ``cvar`` have been applied for this context, so
> ``cvar.value`` gives the default value. Assigning new values to contextvars
> is done in a highly scope-aware manner::
>
> with cvar.assign(new_value):
> assert cvar.value is new_value
> # Any code here, or down the call chain from here, sees:
> # cvar.value is new_value
> # unless another value has been assigned in a
> # nested context
> assert cvar.value is new_value
> # the assignment of ``cvar`` to ``new_value`` is no longer visible
> assert cvar.value == "the default value"
I feel that of "is" and "==" in assert statements in this PEP has to
be used (or described) more precisely.
What if new_value above is 123456789?
maybe using something like could be better? ->
def equals(a, b):
return a is b or a == b
Doesn't PEP need to think about something like "context level overflow" ?
Or members like: cvar.level ?
More information about the Python-ideas
mailing list