Exec and Scope

E

Emanuele D'Arrigo

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
 
J

James Mills

Manu,

Good lord man, what are you trying to solve ?
Describe your "actual problem" you're attempting
to solve... This looks really really ugly and I would
advise against any solution that relies on exec()

--JamesMills
 
R

Rafe

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":
.... def my_command(self):
.... print "hello"
....hello


- Rafe
 
E

Emanuele D'Arrigo

Good lord man, what are you trying to solve ?
Describe your "actual problem" you're attempting
to solve... This looks really really ugly and I would
advise against any solution that relies on exec()

I knew I was going to get some lighting bolt from the community... =)
It wasn't quite elegant, was it? =)

Let's see if I can describe the problem without writing a treaty about
it: I'm trying to implement a command queue manager. It can handle
multiple queues that run in parallel and each queue is made of a list
of arbitrary commands stored as strings. The commands are not
necessarily executed immediately but might have a delay. But commands
in an individual queue are executed in a strict first-in, first-out
order.

The machinery to handle the delays is provided by a third-party API
I'm using. But it only handles actual code, not string commands. But
at this stage when I place a command into the queue, only built-in
commands are executed correctly. Methods that are part of an object
are not. Which make sense: when the command is executed its
environment doesn't know about that particular object. And that's
where I'm stuck. I suspect I need to reshape the whole thing but at
this point I don't quite know how.

Manu
 
E

Emanuele D'Arrigo

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().

I must check this out. My understanding is that getAttr returns a
function object that happens to be a method of that object. I can then
invoke that function as if I was invoking the method of that object.
What I don't know is: will the function work in a scope where the
object is not defined? And even if it does, what happens to the names
that refer to the containeR or containeD objects?

I've read what I could in the manual about the data model, scope and
namespaces but as you can probably see there still are things I do not
fully comprehend. I feel a bit like seeing the dots but not quite
being able to make the connections between them just yet.

Thanks for your help though! This is providing me with a few ideas for
some more tests to do.

Manu
 
E

Emanuele D'Arrigo

Ahh... before you guys reply: I found the way.

Between you James sounding the horn of alarm and you Rafe pointing me
in the right direction I sorted it out. Eventually I didn't end up
using getAttr but looking into it I learned the difference between

myResult = instance.method()

and

myMethod = instance.method

the latter can be passed as an object for execution inside a function,
i.e.:

def myCommand(inputMethod):
inputMethod()

myCommand(myMethod)

and that works flawlessly! Thank you guys!

Manu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top