Question regarding indirect function calls
Maxwell Hammer
hammer at maxwell.com
Sun Apr 13 16:13:21 EDT 2003
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
More information about the Python-list
mailing list