<div dir="auto"><div class="gmail_extra" dir="auto"><div class="gmail_quote">Le 2 janv. 2018 18:57, "Guido van Rossum" <<a href="mailto:guido@python.org">guido@python.org</a>> a écrit :<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>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.)<br></div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">A variable has or has no default value. Would it make sense to expose the default value as a public read-only attribute (it would be equal to _NO_DEFAULT or Token.MISSING if there is no default) and/or add a is_set() method? is_set() returns true if the variable has a default value or if it was set in the "current context".</div><div dir="auto"><br></div><div dir="auto">Currently, a custom sentinel is needed to check if var.get(), ctx.get(var) and ctx[var] would raise an exception or not. Example:</div><div dir="auto"><br></div><div dir="auto">my_sentinel = object()</div><div dir="auto">is_set = (var.get(default=my_sentinel) is not my_sentinel)</div><div dir="auto"># no exception if is_set is true</div><div dir="auto"><br></div><div dir="auto">ContextVar.get() is non obvious because the variable has an optinal default, get() has an optional default parameter, and the variable can be set or not in the current context.</div><div dir="auto"><br></div><div dir="auto">Victor</div><div class="gmail_extra" dir="auto"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div></div></div></blockquote></div></div></div>