3 8 , , , , , ( , , , ), .
[]
, , . , .
Python execfile() builtin. , Python 3.x.
execfile() runme(), spam() eggs() runme(), ( ). :
myscript.py
def spam():
print 'spam'
def eggs():
if 'spam' not in globals():
print 'method spam() is not present in global namespace'
spam()
try:
eggs()
except Exception as e:
print e
mainprogram.py
class mainprogram():
def runme(self):
execfile("myscript.py")
print 'Objects lying in local namespace of runme() are -'
print locals()
this = mainprogram()
this.runme()
>>>import mainprogram
method spam() is not present in global namespace
name 'spam' is not defined
Objects lying in local namespace of runme() are -
{'e': NameError("name 'spam' is not defined",), 'spam': <function spam at 0x000000000000002B>, 'eggs': <function eggs at 0x000000000000002C>, 'self': <mainprogram.mainprogram instance at 0x000000000000002D>}
, spam() , runme(). , , spam()
def eggs():
global this
this.runme.spam()
spam(), runme(). , spam() :
myscript.py
global spam
def spam():
print "spam"
def eggs():
spam()
eggs()
spam() globals() (.. ), eggs().