Proposal: add sys to __builtins__

Steve Holden steve at holdenweb.com
Fri Sep 2 04:09:40 CEST 2005


Rick Wotnaz wrote:
> Michael Hoffman <cam.ac.uk at mh391.invalid> wrote in
> news:df7jlu$1te$1 at gemini.csx.cam.ac.uk: 
> 
> 
>>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.
> 
> 
> +1 here. As far as I'm concerned, both os and sys could be special-
> cased that way. That said, I would guess the likelihood of that 
> happening is 0. 
> 
I wonder if it would be worth special-casing the AttributeError 
exception handling at the outermost lexical scope to try and import a 
module with the troublesome name and then retrying the attribute access 
if the import succeeded (possibly even from a path limited to the 
standard locations).

That way none of the standard library modules would need to be imported 
before use.

I can see that this would create problems as well as solving some, but 
it would be nice not to have to import the standard library modules. I 
don't personally find it a hardship, but some people do.

though-i-could-just-be-raving-ly y'rs  - steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list