Proposal: add sys to __builtins__

Michael Hoffman cam.ac.uk at mh391.invalid
Thu Sep 1 15:09:46 EDT 2005


What would people think about adding sys to __builtins__ so that
"import sys" is no longer necessary? This is something I must add to 
every script I write that's not a one-liner since they have this idiom 
at the bottom:

if __name__ == "__main__":
     sys.exit(main(sys.argv[1:]))

Additionally, the necessity of "import sys" makes some one-liners a 
little more unwieldy than they should be--it is surely the module I am 
missing the most in one-liners. For example, with this proposal, this 
inelegant one-liner:

$ python -c "import sys; print ''.join(sorted(sys.stdin.readlines()))"

could be replaced by:

$ python -c "print ''.join(sorted(sys.stdin.readlines()))"

Since sys is surely the most commonly used module (it is imported in 108 
of 188 Python 2.4 stdlib modules on my system, and certainly more than 
any other module), I would hope few people would be affected by a 
namespace collision.

Other languages (e.g. C#) always make their system namespace available 
without needing a special import.

In short, given the wide use of sys, its unambiguous nature, and the 
fact that it really is built-in already, although not exposed as such, I 
think we would be better off if sys were always allowed even without an 
import statement.
-- 
Michael Hoffman



More information about the Python-list mailing list