Name space quirk?
piet at cs.uu.nl
piet at cs.uu.nl
Mon Jul 23 09:59:51 EDT 2001
>>>>> Alex <new_name at mit.edu> (A) writes:
A> Hi, Gangadhar. I hope I'm addressing you appropriately. :)
A> The issue there is that the "if __name__ == '__main__':" block is not
A> executed when the module is imported, because in that case, "__name__"
A> is bound to the name of the module, in this case, "nspace". As a
A> result, in the "nspace" namespace, nothing is bound to "myfile", so the
A> "myfile.write" command raises a NameError. The "global" keyword only
A> means "global to the module" not "global to the entire program." The
A> "myfile" you bound in the interpreter is in the "__main__" namespace.
A> Try this version:
A> #!/usr/bin/env python
A> from os import sys
A> def hello (outfile = sys.stdout):
A> # We are writing to what should be an
A> # unknown file descriptor:
A> global myfile # <------- Added
A> myfile.write ("Hello, world!\n")
But why do you have a parameter outfile that you don't use. Don't you just
means outfile.write()?
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl
More information about the Python-list
mailing list