Name space quirk?

NANDYALA D Gangadhar n_d_gangadhar at yahoo.com
Sun Jul 22 08:19:34 EDT 2001


Hello,

[Sorry if this is documented and I missed it; I am pretty new to python.]

Running the following as a script goes through (unexpectedly, for me), 
while trying to run it in the interactive python shell catches the 
NameError (on "myfile"):

#!/usr/bin/env python

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

Is this behaviour desirable and correct? I guess it can lead to 
unanticipated results. 

As you expect, I saw this when I happened to change my variable name 
only inside a function, and discovered it after using it a lot of times.

Gangadhar



More information about the Python-list mailing list