Executing functions
Andreas Tawn
andreas.tawn at ubisoft.com
Fri Feb 11 09:41:57 EST 2011
> Can someone help me understand why Example #1 & Example #2 will run
> the functions,
> while Example #3 DOES NOT?
> Thanks for your time!
> R.D.
>
> def One():
> print "running fuction 1"
> def Two():
> print "running fuction 2"
> def Three():
> print "running fuction 3"
>
>
> # Example #1
> fList = ["Two()","Three()"]
> for func in fList:
> exec func
>
> # Example #2
> Two()
> Three()
>
> # Example #3
> fList = ["Two()","Three()"]
> for func in fList:
> func
In example 3, func is a string literal not a function object.
Example 1 works because the exec statement parses and then evaluates the func string resulting in the two function calls you see.
Try this instead...
fList = [One, Two, Three]
for func in fList:
func()
Cheers,
Drea
More information about the Python-list
mailing list