[Python-Dev] PEP 567 pre v3

Victor Stinner victor.stinner at gmail.com
Mon Jan 8 17:35:40 EST 2018


Le 8 janv. 2018 8:36 PM, "Yury Selivanov" <yselivanov.ml at gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180108/5784ca3e/attachment.html>


More information about the Python-Dev mailing list