[Python-ideas] making a module callable

Ben Finney ben+python at benfinney.id.au
Tue Nov 26 00:07:19 CET 2013


Michael Foord <fuzzyman at gmail.com> writes:

> 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.

No, the command name is part of the command line arguments, as I pointed
out earlier. That is, when any program is started with::

    foo bar baz

the command line arguments are “foo”, “bar”, “baz”. That says nothing
about what those arguments *mean* yet; they're all available to the
program, for it to figure out their significance.

> It's only an old C convention (and now an old Python convention) that
> makes you think it is.

Not at all. Giving the command-line arguments to the program as a
sequence of strings is a standard cross-language interface; the
operating system hands the command line to the running program without
caring what language it was implemented in.

This is all prior to talking about what those arguments *mean*; the
discussion of “‘foo’ is a command, ‘bar’ and ‘baz’ are its parameters”
all comes afterward and is irrelevant to the process of *getting at* the
command line.

If the program's main code wants to discard the first argument, as many
programs do for good reasons, that's up to the programmer to decide
explicitly.

Many other programs make use of the whole command line, and ty should
not need some different way to get at the contents of the command line.
If we're going to make the command line sequence a parameter to the main
code, there should be one interface, no special cases. As it stands,
that conventional interface in Python code is::

    def main(argv=None):
        if argv is None:
            argv = sys.argv

and then it's up to the rest of the ‘main’ function (or however it's
spelled) to process the full command line sequence that was received.

So, while the name “argv” is a C convention, the handling of the command
line as a homogeneous sequence of strings is language-agnostic.
Automatically discarding the first argument, on the assumption that the
program doesn't care about it, is making a false assumption in many
cases and makes a common use case needlessly difficult.

-- 
 \       “We must find our way to a time when faith, without evidence, |
  `\    disgraces anyone who would claim it.” —Sam Harris, _The End of |
_o__)                                                     Faith_, 2004 |
Ben Finney



More information about the Python-ideas mailing list