
Oh, the "Specification" section in the PEP is too brief on several of these subjects. It doesn't really specify what var.get() does if the value is not set, nor does it even mention var.get(<default>) except in the code examples for var.reset(). It's also subtle that ctx[var] returns the default (if there is one). I suppose it will raise if there isn't one -- resulting in the somewhat surprising behavior where `var in ctx` may be true but `ctx[var]` may raise. And what does it raise? (All these questions are answered by the code, but they should be clearly stated in the PEP.) I would really like to invite more people to review this PEP! I expect I'll be accepting it in the next two weeks, but it needs to go through more rigorous review. On Thu, Dec 28, 2017 at 4:48 PM, Victor Stinner <victor.stinner@gmail.com> wrote:
NLe 28 déc. 2017 11:20 AM, "Nathaniel Smith" <njs@pobox.com> a écrit :
On Thu, Dec 28, 2017 at 1:51 AM, Victor Stinner <victor.stinner@gmail.com> wrote:
var = ContextVar('var', default=42)
and:
var = ContextVar('var') var.set (42)
behaves the same, no?
No, they're different. The second sets the value in the current context. The first sets the value in all contexts that currently exist, and all empty contexts created in the future.
Oh, that's an important information. In this case, "default" is the best name.
The PEP may be more explicit about the effect on all contexts. Proposition of documentation:
"The optional *default* parameter is the default value in all contexts. If the variable is not set in the current context, it is returned by by context[var_name] and by var.get(), when get() is called without the default parameter."
Victor
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ guido%40python.org
-- --Guido van Rossum (python.org/~guido)