absolute namespace for callbacks?

Mitch Chapman chapman at bioreason.com
Tue Mar 6 12:23:32 EST 2001


Bruce Edge wrote:
> 
> My callback func is defined in one module, and the callback is executed
> from another module which does not know in advance what module the
> callback func resides in. So, by default, the caller cannot find the
> callback func in it's own namespace.

It doesn't need to.  It just needs somebody to pass it a reference
to the callback function so it can save it in its own namespace.

________________________________________________________________________
# Module1.py
def myCB(*args):
    print "Where the heck am I?  Then again, who cares?"
________________________________________________________________________
# Module2.py
_callback = None

def register(newCB):
    global _callback
    _callback = newCB

def notify():
    if _callback:
        _callback()
________________________________________________________________________
# Module3.py
import Module1, Module2

Module1.register(Module2.myCB)
________________________________________________________________________


> The callback registration procedure needs to specify the namespace where
> the callback func exists.

I'd modify that to say, "whoever calls the callback registration 
procedure may need access to the namespace where the callback func 
is defined."  In other words, Module1 and Module2 don't need
to know anything about each other.  Only the composer which glues
the two together needs to know about both, and even it needs only
references to the callback and registration functions.

Does this answer the right question?

--
Mitch Chapman
chapman at bioreason.com



More information about the Python-list mailing list