Velocity Reviews > Executing functions

# Executing functions

DataSmash
Guest
Posts: n/a

 02-11-2011
Can someone help me understand why Example #1 & Example #2 will run
the functions,
while Example #3 DOES NOT?
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

nn
Guest
Posts: n/a

 02-11-2011
On Feb 11, 9:15*am, DataSmash <(E-Mail Removed)> wrote:
> Can someone help me understand why Example #1 & Example #2 will run
> the functions,
> while Example #3 DOES NOT?
> 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

Andreas Tawn
Guest
Posts: n/a

 02-11-2011
> Can someone help me understand why Example #1 & Example #2 will run
> the functions,
> while Example #3 DOES NOT?
> 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.

fList = [One, Two, Three]
for func in fList:
func()

Cheers,

Drea

Ethan Furman
Guest
Posts: n/a

 02-11-2011
DataSmash wrote:
> Can someone help me understand why Example #1 & Example #2 will run
> the functions,
> while Example #3 DOES NOT?
> 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

In this case, func is set to the strings 'Two()' and 'Three()', then the
<exec func> line tells Python to evaluate the strings and execute them.
While this style can be useful, it is also *much* slower than example
2; if all you want is to cycle through the functions, a better way is:

--> fList = [Two, Three]
--> for func in fList:
--> func()

> # Example #2
> Two()
> Three()

The functions Two and Three are called directly

> # Example #2 <-- should be 3
> fList = ["Two()","Three()"]
> for func in fList:
> func

This is not calling func (no () at the end), and in fact doesn't do
anything if called as a script besides evaluate func -- it's a string,
but not being assigned anywhere, so unless you are running from the
interactive prompt where it will be echoed to screen, nothing happens.

~Ethan~

DataSmash
Guest
Posts: n/a

 02-11-2011
Appreciate the responses, guys.
I now see the difference between the ways I was trying to call
function(s).
R.D.