Proposal: add sys to __builtins__

Rick Wotnaz desparn at wtf.com
Mon Sep 5 01:04:41 EDT 2005


"Terry Reedy" <tjreedy at udel.edu> wrote in
news:mailman.120.1125883780.4249.python-list at python.org: 

> 
> "Colin J. Williams" <cjw at sympatico.ca> wrote in message 
> news:JEBSe.55$vN.6924 at news20.bellglobal.com...
>> Rick Wotnaz wrote:
>>> +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.
>>>
>> +1 for both.
> 
> Some people might prefer that math be special cased.  Or some
> other module. A neutral criterion is needed.
> 

Actually, I don't think it's beyond reason to suggest that any 
module included with the standard distribution be special-cased in 
this way. That is, a reference to xxx.func(), without a previous 
import of xxx *could* be resolvable automatically, at least for 
those modules that can be found in a standard location. Whether 
it's a good idea to do so is another question. 

Offhand, I am not sure why it would be an insanely poor idea. It 
would mean some funky namespace building and possibly redundant 
imports, I guess. I'll certainly defer to just about anybody else's 
opinion as to the difficulty and advisability, but I believe it 
would be possible to do. 

Note that I am not saying that a reference to, say, 'argv' should 
provoke an automatic import. I don't mean that some automatic 
search for a matching function name should be done through some 
undefined module chain. I'm talking only about qualified 
references, like os.path or sys.stderr. 

As it is, a NameError is generated if the proper import has not 
been done. At the point of that error, the module name is known. 
For the programmer to fix the situation, an import statement has to 
be added to the code and then the code must be rerun. I don't see 
why it would be impossible to make that happen automatically, 
provided the module to be imported was recognized (through some 
undefined magic). I'd think the module would have to be known, 
because trying to import a nonexistent module -- "import ssy", say 
(in the event of a typo for "sys") -- would fail, so there would 
have to be a way to avoid looping on the "ssy.func()" line.

Is it worth adding that kind of complexity to execution of a Python 
program? Probably not. 

Is it even a good idea to try to make it happen automatically? 
Possibly not. For one thing, it wouldn't always be the right thing 
to do. A developer would most likely want to set up the imports 
properly, and to know when they were not correctly set up instead 
of having Python fix things quietly. There's a reason why Explicit 
is better than Implicit. 

But *could* it be done? I'd think so. 

-- 
rzed



More information about the Python-list mailing list