Name space quirk?

NANDYALA D Gangadhar n_d_gangadhar at yahoo.com
Sun Jul 22 14:47:49 EDT 2001


Alex <new_name at mit.edu> wrote in message news:<etdzo9x16ck.fsf at opus.mit.edu>...
> Hi, Nandyala.  Yeah, that's the correct behaviour.  When it can't find
> "myfile" bound to anything in "hello"'s namespace, it looks in the
> module namespace.
> 
> See the table at http://python.org/doc/ref/execframes.html for more
> details.

Thanks, Alex, for the answer and the reference. Is there any advantage
in such a behaviour? I see much disadvantage.

While at it, now, I cannot get it recognise myfile as a global in the
interactive mode (of course, it works as a script):

nspaces.py:

#!/usr/bin/env python

from os import sys
 
def hello (outfile = sys.stdout):
        # We are writing to what should be an
        # unknown file descriptor:
        global myfile                         # <------- Added
        myfile.write ("Hello, world!\n")
 
if __name__ == '__main__':
        f = sys.argv[0] + ".out"
        myfile = open (f, 'w')
        hello (myfile)

I try:

>>> from nspace import *
>>> dir()
['__builtins__', '__doc__', '__name__', 'hello', 'sys']
>>> myfile = sys.stdout
>>> global myfile
>>> dir()
['__builtins__', '__doc__', '__name__', 'hello', 'myfile', 'sys']
>>> hello()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "nspace.py", line 11, in hello
    myfile.write ("Hello, world!\n")
NameError: global name 'myfile' is not defined

What am I missing? 

Thanks for the patience and help!

Gangadhar

Aside: We Telugu speaking people can be addressed __formally__ also
       by our given names!



More information about the Python-list mailing list