global name space and importing

David Goodger dgoodger at bigfoot.com
Sun May 28 13:09:36 EDT 2000


on 2000-05-28 01:08, Moshe Zadka (moshez at math.huji.ac.il) wrote:
> On Sat, 27 May 2000, Curtis Jensen wrote:
>> How come the global declaration works when I define it from withing the
>> interpreter, but not from within a file that I import?
> 
> Yet another example on why "from foo import *" is evil.
> It imports all the names *currently* in the module. "y" is not in the
> module until you *call* bar.

But even if "y" *were* in the module when imported, and thus imported by
"from foo import *", calling bar() creates a new object bound to "y", so it
dowsn't help. The imported "y" remains bound to the object originally
imported, i.e. before bar() was called. If "y" were a list (or other mutable
object), and bar() changed the object in-place, the sharing of "y" across
namespaces would work.

But it's ugly and will surely come back to bite you!

> Solution: don't do 'from ... import *'

Amen!

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list