473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exec and Scope

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
4 1402
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Toby Donaldson | last post by:
Hi all, I'm designing an educational application that will run Python code and check the output against a pre-define answer. I want to use the "exec" statement to run the code, but I don't know...
5
by: Nick Jacobson | last post by:
This works fine: x = 1 def execfunc(): print x execfunc() So why doesn't this? s = \
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
1
by: Ted | last post by:
-------- def f(): ret = 2 exec "ret += 10" return ret print f() -------- The above example prints '12'. However, the following example prints
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
2
by: N. Pourcelot | last post by:
Hello, I can't understand some specific behaviour of the exec statment. For example, say that I create such a class A : class A: def __init__(self): self.n = 3 self.m = None def h(self,...
3
by: Jens | last post by:
Hi, has anyone an idea why the following code does not work. s = """ def a(n): return n*n
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.