An API to pass data down in the sub-function inspired by React Context
pip install react-context
Inspired by this reddit, here a React Context-like API for Python to pass data down in thesub-function without polluting all the function parameters.
Under the hood it uses inspect.stack().frame
to register the element in the context manager. Frames may be available only in CPython.
To define a context value use create_context(**kargws)
function
with use_context(something=123):
...
To retrieve the value, use get_context(key)
function
def nested_function():
return get_context('something', 'default_value')
with use_context(something=123):
value = nested_function()
print(value) # 123
print(nested_function()) # 'default_value'
To print in the console where a context has been defined in the stacktrace, replace get_context
with debug_context(key, logger=print)