Context module

Provide simple way to passing context between method calls

Context usage:

ctx = Context(value=1)
with ctx as c:
    call_a_function(ctx=c)

A bit more readable version:

ctx = Context(value=1)
with ctx.copywith(value=2, new_value=3) as c:
    call_a_function(ctx=c)
class context.base.Context(*args, **kwargs)[source]

Context provide basic functionality managing through calls. Can be used as a context manager.

Predefined items: global and local

ctx = Context(value=1, global={'value': 1}, local={'value': 1})

with ctx.copywith(value=2, new_value=3) as c:
    ctx['global']['value']  # contains 1
    ctx['local']  # It's an empty dict
    call_a_function(ctx=c)
copywith(*args, **kwds)[source]

Make a copy of itself and pass back to inside context.

Cauction: It uses simple a dict.copy means and if any item itself mutable, there is reflecting outside from the context manager as well. Use immutable values if it’s possible!