On 11 January 2018 at 10:44, Nathaniel Smith <n...@pobox.com> wrote: > It may have gotten lost in that email, but my actual favorite approach > is that we make the signatures: > > ContextVar(name, *, initial_value) # or even (*, name, initial_value) > ContextVar.get() > ContextVar.set(value) > > so that when you create a ContextVar you always state the initial > value, whatever makes sense in a particular case. (Obviously None will > be a very popular choice, but this way it won't be implicit, and > no-one will be surprised to see it returned from get().)
I also like this idea, and I think it aligns better with "dict.get" than it may first appear: the trick is that in this simplified API "ContextVar.get()" would be akin to "get_thread_state().current_context.get(cv, cv.initial_value)" (and PEP 568 wouldn't really change that). So both the key and the default value are specified at ContextVar initialisation time, and if you want to supply a non-standard default, then you need to break the ContextVar abstraction, and access "contextvars.copy_context().get(cv, custom_default)" instead. If we later decide that we do want to support raising an exception for unitialised access after all, then we could introduce a "missing" callback to the ContextVar constructor that would be akin to defaultdict's "default_factory" callback (with the requirement becoming that you have to specify either an initial_value, or a missing callback, but not both). 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/archive%40mail-archive.com