calling functions style question

Kay Schluehr kay.schluehr at gmx.net
Tue Jun 6 11:35:19 EDT 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.




More information about the Python-list mailing list