function namespaces

M.E.Farmer mefjr75 at hotmail.com
Tue Mar 8 19:25:46 EST 2005


Hello Darren,
I am not sure why you are using execfile().
Py> help(execfile)
Help on built-in function execfile:

execfile(...)
    execfile(filename[, globals[, locals]])

    Read and execute a Python script from a file.
    The globals and locals are dictionaries, defaulting to the current
    globals and locals.  If only globals is given, locals defaults to
it.
Do you realize that execfile actually runs your script?

A simple import would probably be the best way to go.
#contents of myfile.py:
testvar = [1,2,3,4]

# someother.py
import myfile
print myfile.testvar

But to answer the question :
#contents of myfile.py:
testvar = [1,2,3,4]

# someother.py
# the problem is you are executing a script
# then searching in the wrong namespace.
def myfunction(filename):
        execfile(filename, globals())
        print testvar 
hth,
M.E.Farmer




More information about the Python-list mailing list