# Python : Core Library : ContextVars \[ [contextvars](https://docs.python.org/3/library/contextvars.html) ] Summary: Create named variables in a context-specific namespace, meaning a separate namespace is provided for each async task. - The `Context` class provides a namespace for contextvars, and implements the Mapping interface. Both it and the `copy_context()` function are generally used by async framework developers rather than app developers. - The `ContextVar` class creates a name variable with optional default value. Provides a token on every call to `set()` than can be used to restore the pre-`set()` value. #### For App Developers **API** ```python ContextVar( name[, default] ) get( [default] ) # return value for current context # if no value -> local default or constructor default or ▲LookupError set( value ) -> Token # token can be used to restore prev value reset( token ) # set to value before the set() call that returned the token Token .var -> ContextVar .old_value # set to Token.MISSING if no value .MISSING ``` **Using** ```python v = ContextVar( "foo", default=42 ) ``` #### For Async Framework Developers ```python copy_context() -> Context # return copy of current context # Mapping of ContextVars Context() # returns empty context # Each thread has its own effective stack of Context objects. # The current context is the Context object at the top of the current thread’s stack. ```