(slightly OT): Python and linux - very cool

Peter Hansen peter at engcorp.com
Fri Aug 2 04:17:23 CEST 2002

"James J. Besemer" wrote:
> Peter Hansen wrote:
> > [describes import bug]
> >
> > Had we done a simple, clean "import ..." instead, the other module
> > would have been referring to the name as modulename.globalname
> > and after initialize() was called it would be bound to the new
> > object instead of to None.
> I submit that in cases like this, where modules rely on
> privacy for the integrity of their operation, then ideally
> they should be designed from the start to be immune
> from "import *" problems such as you describe.
> I realize that many existing modules predate the availability
> of the __all__ keyword.   However, prefixing private vars
> with "_" to prevent them from being imported has been
> available for a long time.  A problem like the one described
> that can be avoided by use of one of the above hiding
> mechanisms may fairly be regarded to be a failing of the
> module designer, rather than the newbie user.

That would not have solved the problem (at least not directly)
in this case, because it was not a _private_ variable that
was involved.  The intention was that it was explicitly a
publicly available variable.  A better solution would have been
to make it available via a function, e.g. getThatGlobal(), 
but simply using _ was not suitable in this case.  

(It would indirectly have solved the problem, of course, because
the programmer would have been forced to figure out another
way to get access to it, but if she knew to use _ in the first
place, she would also have known about the problems "import *"
can cause.)

And the old "never use global variables" comes to mind again too...


More information about the Python-list mailing list