function namespaces

Darren Dale dd55 at cornell.edu
Tue Mar 8 18:36:20 EST 2005


Hi,

I have a variable saved in a file like this

#contents of myfile.py:
testvar = [1,2,3,4]

and I am trying to write a function that does something like this:

def myfunction(filename):
        execfile(filename)
        print testvar

The problem I am running into is that the global name testvar is not
defined, but I dont understand why. I tried calling dir() in the function,
which does list testvar. I tried declaring tesvar a global before calling
execfile, and that didnt help. If I just run execfile('myfile.py') in the
interactive interpretter, testvar is loaded and I can continue my work.

What am I doing wrong? 



More information about the Python-list mailing list