Exec and Scope

Emanuele D'Arrigo manu3d at gmail.com
Thu Oct 30 23:47:25 EDT 2008


Hi everybody!

I'm trying to do something in a way that is probably not particularly
wise but at this point I don't know any better, so bear with me.

Suppose in main.py I have the following statements:

myObject = MyObject()
execThis("myObject.myCommand()")

Now suppose the method

def execThis(aCommandInAString):
    exec(aCommandInAString)

is somewhere "far away" in terms of scope. Somewhere where the code
doesn't know anything about the instance myObject and even less about
its methods and attributes. How do I get myObject.myCommand() properly
executed?

I'm guessing it's about taking a snapshot of or a reference to the
namespace that is correct for the execution of the command, but... is
that the case? And how do I get a handle to that?

Thanks for your help!

Manu



More information about the Python-list mailing list