Le 8 janv. 2018 8:36 PM, "Yury Selivanov" <[email protected]> a
écrit :
2. Context.__contains__, Context.__getitem__ and ContexVar.default
So if we keep the current PEP 567 behaviour w.r.t. defaults,
ContextVar.get() might return a different value from Context.get():
v = ContextVar('v', default=42)
ctx = contextvars.copy_context()
ctx.get(v) # returns None
v.get() # returns 42
v in ctx # returns False
I think this discrepancy is OK. Context is a mapping-like object and
it reflects the contents of the underlying _ContextData mapping
object.
ctx[var] raises an exception but ctx.get(var) returns None in such case. My
point is just that Context.get() behaves differently than dict.get(). If
dict[key] raises, I expect that dict.get() raises too and that I have to
write explicitely dict.get(default=None).
I suggest to modify Context.get() to raise an exception or require to
explicitely write ctx.get(var, default=None).
ContextVar.default is meant to be used only by ContextVar.get().
Context objects should not use it.
I now agree. The difference between ContextVar.get() and Context.get() is
fine and can be explained.
Victor
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com