[Python-Dev] PEP 567 pre v3

Nick Coghlan ncoghlan at gmail.com
Mon Jan 8 22:22:15 EST 2018


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


More information about the Python-Dev mailing list