Executing functions

D

DataSmash

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
 
N

nn

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
 
A

Andreas Tawn

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
 
E

Ethan Furman

DataSmash said:
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

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~
 
D

DataSmash

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top