(slightly OT): Python and linux - very cool

James J. Besemer jb at cascade-sys.com
Thu Aug 1 20:06:17 EDT 2002


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.

> Point is, there
> are definitely cases where you can get in trouble with the "from"
> technique so you'd best avoid it until you know enough Python to
> understand exactly how you might get tripped up.

True.  Don't "import *" unless you KNOW it's safe.

Just don't do it is an even easier rule of thumb.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com






More information about the Python-list mailing list