calling functions style question

Discussion in 'Python' started by Brian, Jun 6, 2006.

  1. Brian

    Brian Guest

    I just have a basic style question here. Suppose you have the program:

    def foo1():
    do something

    def foo2()
    do something else

    Assume that you want to call these functions at execution. Is it more
    proper to call them directly like:

    foo1()
    foo2()

    or in an if __name__ == "__main__": ?

    Both will execute when the script is called directly, I was just
    wondering if there is a preference, and what the pros and cons to each
    method were.

    Thanks,
    Brian
     
    Brian, Jun 6, 2006
    #1
    1. Advertising

  2. The difference becomes clear when you import your program into another
    program (or the command line python editor). __name__!='__main__' when
    you import, so the functions will not be called if they're inside the
    block. This is why you see this block so often at the end of scripts;
    so that the script runs its main functions when called as a standalone
    program, but you can also import the code and do something with it
    without setting off those functions.

    THN

    Brian wrote:
    > I just have a basic style question here. Suppose you have the program:
    >
    > def foo1():
    > do something
    >
    > def foo2()
    > do something else
    >
    > Assume that you want to call these functions at execution. Is it more
    > proper to call them directly like:
    >
    > foo1()
    > foo2()
    >
    > or in an if __name__ == "__main__": ?
    >
    > Both will execute when the script is called directly, I was just
    > wondering if there is a preference, and what the pros and cons to each
    > method were.
    >
    > Thanks,
    > Brian
     
    Thomas Nelson, Jun 6, 2006
    #2
    1. Advertising

  3. Brian

    Kay Schluehr Guest

    Brian wrote:
    > I just have a basic style question here. Suppose you have the program:
    >
    > def foo1():
    > do something
    >
    > def foo2()
    > do something else
    >
    > Assume that you want to call these functions at execution. Is it more
    > proper to call them directly like:
    >
    > foo1()
    > foo2()
    >
    > or in an if __name__ == "__main__": ?
    >
    > Both will execute when the script is called directly, I was just
    > wondering if there is a preference, and what the pros and cons to each
    > method were.
    >
    > Thanks,
    > Brian


    If you want those functions to be called each time your module gets
    imported you have to apply calls out of the "if __name__ ..."
    statement. If your module is, for certain reasons, always the __main__
    module and never gets imported there is no obvious preference because
    behaviour will be the same.
     
    Kay Schluehr, Jun 6, 2006
    #3
    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. Daniel Vallstrom
    Replies:
    2
    Views:
    2,050
    Kevin Bracey
    Nov 21, 2003
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,987
    Eric Sosman
    Jan 7, 2005
  3. Ross A. Finlayson
    Replies:
    19
    Views:
    652
    Keith Thompson
    Mar 10, 2005
  4. Khookie
    Replies:
    23
    Views:
    1,665
    Tor Rustad
    Dec 9, 2007
  5. Ken Varn
    Replies:
    0
    Views:
    537
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page