How to pass a reference to the current module

Paul Rubin http
Fri Aug 3 22:01:04 EDT 2007


James Stroud <jstroud at mbi.ucla.edu> writes:
>       Module            Behavior
>    ==============  ====================================================
>     UserDefined1   Imports FunctionUser
>     ThirdParty     Contains User Functions (May be ==UserDefined1)
>     FunctionUser   do_something_with() and/or get_function_from_name()
> 
> So the name-to-function mapping is done in FunctionUser but the
> function is actually defined in UserDefined1 (or ThirdParty if
> ThirdParty is different than UserDefined1).

I'm still completely confused.  Does FunctionUser know what module the
user functions are supposed to come from?  Could you give an example
of what you want the actual contents of those 3 modules to look like?
E.g.:

    UserDefined1.py:
      import FunctionUser

      def foo(x):
          print x+3

    FunctionUser.py:
       def do_something_with (module, funcname, *args, **kw):
         func = getattr(module, funcname)
         func (*args, **kw)

    main.py:
       import UserDefined1, FunctionUser

       # the following should print 10
       FunctionUser.do_something_with(UserDefined1, 'foo', 7)

I don't think the above is quite what you want, but is it somewhere
close?



More information about the Python-list mailing list