Executing functions

Discussion in 'Python' started by DataSmash, Feb 11, 2011.

  1. DataSmash

    DataSmash Guest

    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
     
    DataSmash, Feb 11, 2011
    #1
    1. Advertising

  2. DataSmash

    nn Guest

    On Feb 11, 9:15 am, DataSmash <> 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
     
    nn, Feb 11, 2011
    #2
    1. Advertising

  3. DataSmash

    Andreas Tawn Guest

    > 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
     
    Andreas Tawn, Feb 11, 2011
    #3
  4. DataSmash

    Ethan Furman Guest

    DataSmash 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


    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~
     
    Ethan Furman, Feb 11, 2011
    #4
  5. DataSmash

    DataSmash Guest

    Appreciate the responses, guys.
    I now see the difference between the ways I was trying to call
    function(s).
    R.D.
     
    DataSmash, Feb 11, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmFzIE1hbmdoZXJh?=

    ASP.NET not executing interop functions

    =?Utf-8?B?SmFzIE1hbmdoZXJh?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    469
    =?Utf-8?B?SmFzIE1hbmdoZXJh?=
    Apr 6, 2005
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,663
    Victor Bazarov
    Jun 21, 2004
  3. Replies:
    2
    Views:
    601
  4. HMS Surprise

    Executing a list of functions

    HMS Surprise, Mar 16, 2007, in forum: Python
    Replies:
    6
    Views:
    303
    Alex Martelli
    Mar 19, 2007
  5. Mithil
    Replies:
    3
    Views:
    2,374
    Mithil
    Apr 12, 2007
Loading...

Share This Page