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 <ncogh...@gmail.com> wrote: > On 9 January 2018 at 05:34, Yury Selivanov <yselivanov...@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 | ncogh...@gmail.com | Brisbane, Australia > _______________________________________________ > Python-Dev mailing list > Python-Dev@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)
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com