How am I doing?
Mike Meyer
mwm at mired.org
Mon Sep 19 22:48:52 EDT 2005
"Brett Hoerner" <bretthoerner at gmail.com> writes:
> Wouldn't the standard idiom be to actually put the code under the
> if-name, and not make a whole new main() function?
Depends on how big the main() function is. By making it a function,
you make it possible for other modules to run it directly. In
particular, if foo.py goes into the python library, then "foo" can be:
#!/usr/bin/env python
from foo import main
main()
which means your main gets the (rather trivial) benefit of being
precompiled.
> I'm not sure I see the reason behind main(), couldn't that also
> interfere with other modules since main() seems like it might be
> common, not sure how it would work as I'm pretty new to Python myself.
> from script import * ... what happens when you have two main()s?
You can't have two main()s. You can *define* two main()s, but only one
of them can be bound to that name. So yes, if you do "from foo import *",
then you can lose the binding to one of the mains. But if your code
isn't in a routine, you can't get to it by name anyway. In any case,
"from foo import *" is frowned upon - it makes it hard to figure out
where the variables in the namespace you do that in came from.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list