"Megan Land" <ml...@us.ibm.com> wrote
I'm trying to call a function from a dictionary. I did some googling and
from what I can tell my code should work, but doesn't. Here's an
example:
def myFunc(self, inputList):
The fact you have a self in there suggests that this is a method
of some class? Is it? If not remove the self.
dict={0: func0, 1: func1, 2:func2}
What do you think this is doing? What are func0, func1 etc?
Where are they defined?
for element in inputList:
dict[element]()
This will work provided element exists in dict and dict[element]
is a callable object, eg a function. But I recommend putting
some error handling in for the cases where either of those
two conditions is not true
When I go to run this I get an error saying func0 is not defined. Does
anyone have any ideas as to why this won't work?
Because you haven't defined func0!
try adding
def func0(): print 'func0'
def func1(): print 'func1'
def func2(): print 'func2'
above your function.
Then it might work if you call you function like this:
myFunc([func2,func0,func1])
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor