global name space and importing
Moshe Zadka
moshez at math.huji.ac.il
Sun May 28 01:08:50 EDT 2000
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? See following
> example.
>
> file: foo_bar.py:
> def foo(i):
> x = i
>
> def bar(i):
> global y
> y = i
>
> Python interpreter:
> >>> from foo_bar 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.
>>> from foo import *
>>> bar()
>>> from foo import *
>>> y
1
Where
# foo.py
def bar():
global y
y = 2
Solution: don't do 'from ... import *'
--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com
More information about the Python-list
mailing list