[Python-Dev] PEP 567 pre v3

Guido van Rossum guido at python.org
Mon Jan 8 23:04:24 EST 2018


When I +1'ed Yury's message I forgot about this issue. I actually prefer
the current PEP 567 version -- .get() raises an error if there's no default
on the ContextVar, and .get(None) returns None if there's no default. The
idea here is that by far the most common use will be .get(), so it should
be a short name. (In that sense it's similar to the Queue API.)



On Mon, Jan 8, 2018 at 7:22 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 9 January 2018 at 05:34, Yury Selivanov <yselivanov.ml at gmail.com>
> wrote:
> > Maybe we can rename ContextVar.get() to ContextVar.lookup()?  This
> > would help to avoid potential confusion between Context.get() and
> > ContextVar.get().
>
> I think this would also tie in nicely with the PEP 568 draft, where
> "ContextVar.lookup()" may end up scanning a chain of Context mappings
> before falling back on the given default value.
>
> That said, I do wonder if this may be a case where a dual API might be
> appropriate (ala dict.__getitem__ vs dict.get), such that you have:
>
>     ContextVar.get(default=None) -> Optional[T] # Missing -> None
>     ContextVar.lookup() -> T # Missing -> raise LookupError
>
> If you set a default on the ContextVar itself, they'd always be
> identical (since you'll never hit the "Missing" case), but they'd
> mimic the dict.__getitem__ vs dict.get split if no var level default
> was specified.
>
> The conservative option would be to start with only the
> `ContextVar.lookup` method, and then add `ContextVar.get` later if
> it's absence proved sufficiently irritating.
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at 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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180108/be65cd28/attachment-0001.html>


More information about the Python-Dev mailing list