On Oct 31, 10:47 am, "Emanuele D'Arrigo" <[EMAIL PROTECTED]> wrote: > 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
If you are just looking to execute an attribute (be it a property, module-level function, instance or class method, or anything else which is an attribute of an object), just use getattr(). Execute a 'method' (which is just a callable object right?) of an instance of MyObject named "myCommand": >>> class MyObject(object): ... def my_command(self): ... print "hello" ... >>> myObject = MyObject() >>> attr = getattr(myObject, "my_command") >>> attr() hello - Rafe -- http://mail.python.org/mailman/listinfo/python-list