On 17 October 2017 at 15:02, Nick Coghlan <ncogh...@gmail.com> wrote:
> On 17 October 2017 at 14:31, Guido van Rossum <gu...@python.org> wrote: > >> No, that version just defers to magic in ContextVar.get/set, whereas what >> I'd like to see is that the latter are just implemented in terms of >> manipulating the mapping directly. The only operations for which speed >> matters would be __getitem__ and __setitem__; most other methods just defer >> to those. __delitem__ must also be a primitive, as must __iter__ and >> __len__ -- but those don't need to be as speedy (however __delitem__ must >> really work!). >> > > To have the mapping API at the base of the design, we'd want to go back to > using the ContextKey version of the API as the core primitive (to ensure we > don't get name conflicts between different modules and packages), and then > have ContextVar be a convenience wrapper that always accesses the currently > active context: > > class ContextKey: > ... > class ExecutionContext: > ... > > class ContextVar: > def __init__(self, name): > self._key = ContextKey(name) > > def get(self): > return get_execution_context()[self._key] > > def set(self, value): > get_execution_context()[self._key] = value > > def delete(self, value): > del get_execution_context()[self._key] > Tangent: if we do go this way, it actually maps pretty nicely to the idea of a "threading.ThreadVar" API that wraps threading.local(): class ThreadVar: def __init__(self, name): self._name = name self._storage = threading.local() def get(self): return self._storage.value def set(self, value): self._storage.value = value def delete(self): del self._storage.value (Note: real implementations of either idea would need to pay more attention to producing clear exception messages and instance representations) 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