By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Exec and Scope

P: n/a
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
Oct 31 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

On Fri, Oct 31, 2008 at 1:47 PM, Emanuele D'Arrigo <ma****@gmail.comwrote:
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
--
http://mail.python.org/mailman/listinfo/python-list


--
--
-- "Problems are solved by method"
Oct 31 '08 #2

P: n/a
On Oct 31, 10:47*am, "Emanuele D'Arrigo" <man...@gmail.comwrote:
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
Oct 31 '08 #3

P: n/a
On Oct 31, 4:38*am, Rafe <rafesa...@gmail.comwrote:
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

Oct 31 '08 #4

P: n/a
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
Oct 31 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.