Re: no sys.argc

Steven D. Majewski (
Tue, 28 Jun 1994 18:46:42 -0400

On Jun 28, 15:51, Mike Tibbs wrote:
> Why does the sys module have "argv" but no "argc"?

It would be redundant, since there *is* "len(sys.argv)".

> I have to do:
> if len(sys.argv) == 1:
> print 'Usage..."
> instead of:
> if sys.argc == 1:
> print 'Usage...'
> Just wondering...

If there was a sys.argc, then you would have to remember to
update its value whenever you changed sys.argv.

While this may not be common in C, it's not unusual in Python.

In C, argc and argv are arguments of the main routine.
In Python, 'argv' is available to any module that imports 'sys'.

[ The only case I can recall of needing to modify argv (in C) was
on non unix systems like VMS where you wanted to emulate some
UNIX shell feature: There was an external main routine that
called the C main() function, so it was possible to insert
code to scan for redirection arguments ( NOT intercepted by
the shell if you didn't run a posix shell ) and act on them
before main() was called. ]

You may have a preprocessing module that scans sys.argv,
removing arguments that it can handle, and leaving others
to be processed by the next module.

Or you may load sys.argv with values interactively before
importing a module that gets it's args from sys.argv.
( This last is now a bit less common now that you can use
"if __name__ == '__main__':" to check for stand-alone-ness )

But, even if those aren't good enough reasons to want to muck
with the value of sys.argv, since it is NOT a read-only value,
and it is accessable from any other module (that imports sys),
there is no way you can ensure that it's lenght will remain
constant, so it would only tempt fate and confusion to make
it appear to have a constant value stored in sys.argc.

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics