[Python-ideas] sys.args

Andrew Barnert abarnert at yahoo.com
Sat Jul 27 03:03:18 CEST 2013



Sent from a random iPhone

On Jul 27, 2013, at 0:08, Chris Angelico <rosuav at gmail.com> wrote:

> On Fri, Jul 26, 2013 at 5:40 PM, Ryan <rymg19 at gmail.com> wrote:
>> But that violates the Python Zen: 'There should be one, and preferably only
>> one, way to do it'. The name argv isn't meaningless. I believe it comes from
>> C. argv was the argument vector, i.e. the container for the arguments.
> 
> The name comes from the C pair of argc/argv, where you need a separate
> integer count of args. So it's not wholly appropriate to Python.
> Ironically, it's not even mandated by C (it's just a convention,
> equivalent to Python methods having a first argument named 'self'),

While the names of the arguments to main aren't mandated by C or POSIX, the names of the arguments to related functions ranging from getopt to execv are mandated by one or the other, and universally argc and argv. (For main, those names _are_ used in the signature and the text, but with the parenthetical comment "referred to here as argc and argv, although any names may be used, as they are local to the function".)

The funny thing is that the C standard goes out of its way to avoid using the words "count" and "vector", and even "argument". There's some pretty convoluted verbiage to explain that argc is a count without ever saying so. 

Interestingly, Python's higher level wrappers like subprocess and argparse, and even some of the lower level thing wrappers, use args instead. So there might be some precedent for this idea if sys.argv were considered a higher-level interface. 

But of course the higher level interface is argparse or fileinput. IIRC the argv docs even recommend using one of them in place of trying to process arguments manually. And if not… maybe the appropriate fix is that they should?

> but it's a fixed name in Python, where it's inaccurate. But it's
> something that's found in many other languages too, so it's something
> plenty of people will understand (particularly as 'argv' (almost)
> invariably refers to the program's arguments,

Well, it also often refers to related things--if the list I pass to subprocess, the fake args I pass to argparse for testing, etc. are stored in a variable in my code, it's probably called argv... But that just strengthens your point.


More information about the Python-ideas mailing list