How am I doing?
Jason
jason at jasonmhirst.co.uk
Tue Sep 20 08:42:06 EDT 2005
Tom Anderson wrote:
> On Mon, 19 Sep 2005, Brett Hoerner wrote:
>
>> Wouldn't the standard idiom be to actually put the code under the
>> if-name, and not make a whole new main() function?
>
> Yes.
>
> The nice thing about the main() function, though, is that you can do the
> most basic argument parsing in the if-block. Like so:
>
> def powers(n):
> m = 1
> while True:
> yield m
> m = m * n
>
> def main(n, limit):
> for power in powers(n):
> if (power > limit): break
> print power
>
> import sys
>
> if (__name__ == "__main__"):
> main(int(sys.argv[1]), int(sys.argv[2]))
>
> That serves as a sort of documentation on the arguments to the script, and
> also makes it easier for other scripts to reuse the main logic of the
> program, since they don't have to package parameters up as a string array.
> It is more verbose, though.
>
>> 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.
>
> The two mains would be in different namespaces, so they wouldn't conflict.
>
>> from script import *
>
> Don't do that. 'from script import x' is, IMNERHO, bad practice, and 'from
> script import *' is exceptionally bad practice. I know a lot of people do,
> but that doesn't make it right; namespaces are there for a reason.
>
> tom
>
I haven't a clue what all this means, but it looks important ! lol
Thanks for the headsup, will take note of what you've said.
Incidentally, at work my main programming platform is VisualStudio .Net,
and I never import the children of namespaces so hopefully this practice
I have will be carried over to Python.
More information about the Python-list
mailing list