Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > calling functions style question

Reply
Thread Tools

calling functions style question

 
 
Brian
Guest
Posts: n/a
 
      06-06-2006
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

 
Reply With Quote
 
 
 
 
Thomas Nelson
Guest
Posts: n/a
 
      06-06-2006
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


 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      06-06-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C style: private functions, function docstrings & style Khookie C Programming 23 12-09-2007 01:40 AM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM



Advertisments