global name space and importing

Curtis Jensen cjensen at bioeng.ucsd.edu
Sat May 27 20:47:51 EDT 2000


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 *
>>> bar(0)
>>> print y
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: y
>>> def other(i):
...   global z
...   z = i
...
>>> other(5)
>>> print z
5
>>> ^D


-- 
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451



More information about the Python-list mailing list