Question regarding indirect function calls 
Author Message
 Question regarding indirect function calls

I am just starting to learn python but I love it already..
Ok the question..
In the program I'm trying to write I have a module called "plugins"
containing a number of functions. I only know at runtime the name of a
function to call from "plugins", i.e. need to call a function indirectly.
The problem is that assigning the name of one of plugin's functions to a
variable, I cannot then call the function. IOW:
 func=function1
 plugins.func() ..........will fail
 plugins.function1() .......will pass

Perhaps the following code snippet explains it better...

 <example>
 import plugins # <--------module containing functions I want to call
                # indirectly, say function "function1"

 def test(job)
 # determine whether the job binary is either an internal plugin
 # or an exernal executable
 job_bin = string.strip(job[2])  <---the name of the actual job is here

 if os.access(job_bin, os.X_OK):
   print "external job found"  # job binary is an external program
 else:
   # find out which names our "plugin" module defines
   # and try to find our job in that list of names
   for element in dir(plugins):
    if job_bin == element:
      print "found match for job_bin: ",element
      if callable(plugins.function1):
        print "explicit call: job is callable" <-----------explicit call works
        try:
          plugins.job_bin <---------------------implicit call fails
          print "implicit call: job is callable"
        except:
          print "implicit call: job is not callable"
      else:
        print "explicit call: job is NOT callable"

To show what works and doesn't here's the sample output:
  found match for function1
  explicit call: job is callable
  implicit call: job is not callable

The above is for illustration only to show that I can only call
plugins.function1 directly. I have read python.org docs and am also using
book "The Complete Python Reference", but no where can I find the reason
why my code fails.

Thanks in advance for your help



Fri, 30 Sep 2005 04:13:21 GMT  
 Question regarding indirect function calls

Quote:

> I am just starting to learn python but I love it already..
> Ok the question..
> In the program I'm trying to write I have a module called "plugins"
> containing a number of functions. I only know at runtime the name of a
> function to call from "plugins", i.e. need to call a function indirectly.
> The problem is that assigning the name of one of plugin's functions to a
> variable, I cannot then call the function. IOW:
>  func=function1
>  plugins.func() ..........will fail
>  plugins.function1() .......will pass

getattr(plugins, func)()

should do what you desire.

Alex



Fri, 30 Sep 2005 05:57:56 GMT  
 Question regarding indirect function calls
Quote:

> In the program I'm trying to write I have a module called "plugins"
> containing a number of functions. I only know at runtime the name of a
> function to call from "plugins", i.e. need to call a function indirectly.
> The problem is that assigning the name of one of plugin's functions to a
> variable, I cannot then call the function. IOW:
>  func=function1
>  plugins.func() ..........will fail
>  plugins.function1() .......will pass

<snipped code>

What you are actually doing is this
  func = "function1"
  plugins.func()
which doens't work in the same way this doesn't work
  plugins."function1"()

Try one of these
  getattr(plugins, "function1")()
  plugins.__dict__["function1"]()

hope that helps,

-jackdied



Fri, 30 Sep 2005 05:16:31 GMT  
 Question regarding indirect function calls
In reply to: Jack & Alex:

Thanks for your advice, I did get it working using "getattr" as you
suggested.

Great! now I have another questions, since I have your attention :-)

What is the most efficient way of accessing global variables across
modules?

I have main.py which parses a config file and sets up config values
appropriately. Different values are required by different functions in
other modules.

I have tried defining the config values as global in main, AND in the
other modules - but does not work (get undefine errors).
main.py:
  global config_values
  config_values = []
...
 load config_values
...

Module x
 global config_values

def function1():
  try to access config_values[0] fails - not found

So what I am currntly doing is passing what ever config values are needed
through the function call
  x.function1(config_values, etc)

How would you do this??

Once again thanks for help with indirect calls.



Fri, 30 Sep 2005 11:01:21 GMT  
 Question regarding indirect function calls

Quote:

> In reply to: Jack & Alex:

> Thanks for your advice, I did get it working using "getattr" as you
> suggested.

> Great! now I have another questions, since I have your attention :-)

> What is the most efficient way of accessing global variables across
> modules?

Just do so _explicitly_.

Quote:
> I have main.py which parses a config file and sets up config values
> appropriately. Different values are required by different functions in
> other modules.

> I have tried defining the config values as global in main, AND in the
> other modules - but does not work (get undefine errors).

Each module's globals belong to that module only -- other modules
can access them explicitly.

Quote:
> main.py:
>   global config_values
>   config_values = []
> ...
>  load config_values
> ...

> Module x
>  global config_values

> def function1():
>   try to access config_values[0] fails - not found

Change to:
module x:

import main
def function1()
    access main.config_values[0]

Quote:
> So what I am currntly doing is passing what ever config values are needed
> through the function call
>   x.function1(config_values, etc)

This is quite a good approach, too -- even more explicit.

Yet another alternative is to use "from main import config_values" in
module x, but I do NOT recommend this -- just "import main" and explicit
use of "main.config_values" is FAR better, both stylistically and in
terms of functionality.  For example, with the "from" statement, you
may become vulnerable to the case in which module x is loaded before
(or worse _during_...) module main -- or to situations where at some
later point module main rebinds its config_values name to a different
value -- and so on.  The "import" statement, with the explicit use of
"main.config_values", is more resilient, as well as more explicit and
thus clearer and more readable.

Quote:
> How would you do this??

Probably with "import main" and using main.config_values, although
explicit arguments can also be an excellent alternative.

Alex



Fri, 30 Sep 2005 15:36:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. indirect function calls and variable variables

2. Indirect function member calls

3. indirect function calls and variable variables

4. VC++ calling fortran function and fortran function calling a c++ function

5. A question regarding array-valued functions in fortran 90

6. LISP question regarding interactive functions

7. Q: Interfacing C/ADA: Indirect Calls

8. Indirect calls using MASM 6.1 (how?)

9. indirect call with MASM?

10. Help - indirect call

11. How to find out name of calling function from called function

12. Indirect calls

 

 
Powered by phpBB® Forum Software