Yury Selivanov wrote:

> I suggest you to open an issue on bugs.python.org to implement support
> for context manager protocol for contextvars.Token.  I'm not opposed
> to the idea.  Keep in mind that Python 3.8 is already in a feature
> freeze mode, so the earliest we can get this is Python 3.9.

I'll do that.  Le me just raise one more aspect: for the usage of
context vars as configuration variables (and likely for other uses as
well), it would be useful if there was some way to validate new values
when they are set.  This would provide immediate feedback to the user
and would avoid having to check them at every use.

Did you consider anything like that?  For example, ContextVar could
accept an optional keyword arg 'validate' that must be a function that
is then called for each new value, and somehow reports problems (by
return value or by raising an exception).

(A somewhat related issue would be support for ContextVars in the typing
module.)

> The contextvars module uses a special dictionary implementation (read
> more on that in PEP 567/550) with its ".set()" operation only slightly
> slower than updating a standard Python dict. Setting/resetting N
> context variables is about 1.5x slower than mutating a Python dict N
> times.  In short, it's a fast operation by Python standards.

Thanks.

> > A final, unrelated comment: I find the use of the word "context" in the
> > standard library for both context managers and context variables (and
> > related to them "contexts") needlessly confusing. (...)
>
> Pull requests to improve the docs are always welcome!

Sure, I just wanted to double check whether I'm overseeing anything here
or whether both kinds of contexts are indeed completely independent
concepts.
Python-Ideas mailing list -- python-dev(a)python.org
To unsubscribe send an email to python-ideas-leave(a)python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/

Reply via email to