[Python-ideas] making a module callable

Michael Foord fuzzyman at gmail.com
Mon Nov 25 13:26:40 CET 2013


On 25 November 2013 03:58, Ben Finney <ben+python at benfinney.id.au> wrote:

> Greg Ewing <greg.ewing at canterbury.ac.nz> writes:
>
> > Ben Finney wrote:
> > > It's more convenient to look in “the sequence of command-line
> > > parameters” for all the command-line parameters, without
> > > special-casing the command name.
> >
> > But you hardly ever want to process argv[0] the same way as the rest
> > of the arguments, so you end up treating it as a special case anyway.
>
> This isn't about *processing* that argument; it's about *receiving* it
> in the first place to the function. Having it omitted by default means
> there's a special case just to *get at* the first command-line
> argument::
>
>
The name of the script is not an argument *to* the script. Having it there
in the first place is the special case, not removing it. It's only an old C
convention (and now an old Python convention) that makes you think it is.

Michael Foord



>     def __main__(argv_without_first_arg=None):
>         if argv is None:
>             argv = sys.argv[1:]
>         first_arg = sys.argv[0]
>
> Which still sucks, because how do I then pass a different command name
> to ‘__main__’ since it now expects to get it from elsewhere?
>
> Much better to have the interface just accept the *whole* sequence of
> command line arguments:
>
>     def __main__(argv=None):
>         if argv is None:
>             argv = sys.argv
>
> Now it's completely up to the caller what the command-line looks like,
> which means the ‘__main__’ code needs no special cases for using the
> module as a library or for unit tests etc. You just construct the
> command-line as you need it to look, and pass it in to the function.
>
> > It seems to me we only think of it as a command line argument because
> > C traditionally presents it that way.
>
> What C does isn't relevant here. I think of the whole command line as a
> sequence of arguments because that's how the program receives the
> command line from the Python interpreter. Mangling it further just makes
> a common use case more difficult for no good reason.
>
> > I don't think it's something that would naturally come to mind
> > otherwise. I know I found it quite surprising when I first encountered
> > it.
>
> Many useful things are surprising when one first encounters them :-)
>
> --
>  \        “I don't accept the currently fashionable assertion that any |
>   `\       view is automatically as worthy of respect as any equal and |
> _o__)                                   opposite view.” —Douglas Adams |
> Ben Finney
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131125/6cca4c1f/attachment.html>


More information about the Python-ideas mailing list