python late binding - dynamically put locals in scope
i have a function, m_chain, which refers to two functions `bind` and `unit` which are not defined. i want to wrap this function in some context which provides definitions for these functions - you can think of them as interfaces for which i want to dynamically provide an implementation.

    def m_chain(*fns):
        """what this function does is not relevant to the question"""
        def m_chain_link(chain_expr, step):
            return lambda v: bind(chain_expr(v), step)
        return reduce(m_chain_link, fns, unit)

In Clojure, this is done with macros. what are some elegant ways of doing this in python? i have considered:

 - polymorphism: turn m_chain into a method referring to `self.bind` and `self.unit`, whose implementations are provided by a subclass
 - implementing the `with` interface so i can modify the environment map and then clean up when i'm done
 - changing the signature of m_chain to accept unit and bind as arguments
 - requiring usage of m_chain be wrapped by a decorator which will do something or other - not sure if this even makes sense

ideally, i do not want to modify m_chain at all, i want to use the definition as is, and all of the above options require changing the definition. This is sort of important because there are other m_* functions which refer to additional functions to be provided at runtime.
python late binding - dynamically put locals in scope
===

i have a function, m_chain, which refers to two functions `bind` and `unit` which are not defined. i want to wrap this function in some context which provides definitions for these functions - you can think of them as interfaces for which i want to dynamically provide an implementation.

    def m_chain(*fns):
        """what this function does is not relevant to the question"""
        def m_chain_link(chain_expr, step):
            return lambda v: bind(chain_expr(v), step)
        return reduce(m_chain_link, fns, unit)

In Clojure, this is done with macros. what are some elegant ways of doing this in python? i have considered:

 - polymorphism: turn m_chain into a method referring to `self.bind` and `self.unit`, whose implementations are provided by a subclass
 - implementing the `with` interface so i can modify the environment map and then clean up when i'm done
 - changing the signature of m_chain to accept unit and bind as arguments
 - requiring usage of m_chain be wrapped by a decorator which will do something or other - not sure if this even makes sense

ideally, i do not want to modify m_chain at all, i want to use the definition as is, and all of the above options require changing the definition. This is sort of important because there are other m_* functions which refer to additional functions to be provided at runtime.