Executing functions
nn
pruebauno at latinmail.com
Fri Feb 11 09:31:58 EST 2011
On Feb 11, 9:15 am, DataSmash <r... at new.rr.com> wrote:
> 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 #2
> fList = ["Two()","Three()"]
> for func in fList:
> func
Example 1 is executing the code inside strings
Example 2 is evaluating two functions and throwing the result away
Example 3 is evaluating literals and throwing the result away
I don't know why you would expect the third example to run the
functions.
Maybe running this version will enlighten you:
fList = ["random","stuff"]
for func in fList:
func
More information about the Python-list
mailing list