Newbie: opening files.

Thomas Wouters thomas at xs4all.net
Fri May 19 18:31:20 EDT 2000


On Fri, May 19, 2000 at 12:01:37PM -1000, Doug Stanfield wrote:

> I was only hoping to illustrate what the issue is.  You've probably helped,
> but I'm still not sure what will make the "ahaaa" bomb go off for others.
> Only remembering it took me a while. :-/  This is namespace pollution,
> right?

'from <module> import *' is namespace pollution, yes. For some modules, it's
actually easier to use it (like Tkinter) and some people advise it, but
using it on modules like os, sys, and other modules that do not take care
with what symbols they define (because they dont expect to be import-*'ed),
is enough namespace pollution to scare greenpeace. (Who wants to start
namespeace ? :-) Sorry, bad pun, it's late :)

Personally, I try not to use 'from <module> import *' even with Tkinter --
this means my 'from Tkinter import Tk, Frame, Button, ...' line can get a
tad long in small programs, but it makes me feel a lot better when reading
the code. (For larger programs, I just use 'import Tkinter', but then, most
larger programs subclass Tkinter classes, so it doesn't mean that much more
writing.)

The only real use of 'from <module> import *', in my opinion, can be found
in os.py:

    from posix import *
(or 'nt', or 'os2', or 'dos', or 'mac')

That is, when you are working with several modules, all of which are yours,
it's safe to import all symbols from another module. For instance, Mailman
uses this: it has two modules with settings, Default.py, and mm_cfg.py.
Default.py comes with the distribution (and gets modified by configure to
fit your local system) containing all possible configuration values, with
standard defaults. mm_cfg.py imports all symbols from Defaults.py, and
possibly overwrites them. The mailman administrator only ever edits
mm_cfg.py, adjusting values there, and when Mailman is upgraded, the old
mm_cfg.py can remain intact, and new defaults for new config values are
automatically included, from Defaults.py.

But in general, when qualifying the values imported from the module with the
module name is too much typing, and you have too many values from the module
you want to use, and you dont want to do something like this:

import verylongmodulenameforsomereasonorother
m = verylongmodulenameforsomereasonorother

-- it's probably better to hire a typist, instead of using 'from
verylongmodulenameforsomereasonorother import *' :-)

note-the-number-of-options-though-ly y'rs,

PS: I've been thinking about this sort of thing a lot, now that some of my
co-workers are looking into Python... I wonder if there should be a lengthy
explanation on this somewhere. Sometimes I even wonder if there already is
one :-) My problem is that it is so incredibly obvious to me, I sometimes
have trouble explaining it to people who think just a teensy bit different.
How do other people cope with this ? :)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list