from box import Box, singleton, threadlocal box = Box() box.put('wow', factory=create_wow, scope=my_scope_fn) box.put('foo', factory=create_foo, scope=singleton) box.put('bar', 42) @box.pass('foo') # lazy passing @box.pass('bar', as='b') # lazy passing def do_something(a, b): return a + b box.get('bar') do_something() do_something(13) do_something(13, 42) # ------------ # ------------ # without populated box do_something() # exception do_something(13) # exception do_something(13, 42) # works as everything explicitly passed box.get('bar') # keyerror exception # ------------ # ------------ from box import current_box # the technique is similiar to flask @current_box.pass('foo') def do_something(foo): return foo box1 = Box() box2 = Box() box1.put('foo', 'a') box2.put('foo', 'b') with box1: do_something() # -> a with box2: do_something() # -> b