Exec and Scope

Discussion in 'Python' started by Emanuele D'Arrigo, Oct 31, 2008.

  1. 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
    Emanuele D'Arrigo, Oct 31, 2008
    #1
    1. Advertising

  2. Emanuele D'Arrigo

    James Mills Guest

    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 <> 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
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    --
    -- "Problems are solved by method"
    James Mills, Oct 31, 2008
    #2
    1. Advertising

  3. Emanuele D'Arrigo

    Rafe Guest

    On Oct 31, 10:47 am, "Emanuele D'Arrigo" <> 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
    Rafe, Oct 31, 2008
    #3
  4. On Oct 31, 3:56 am, "James Mills" <>
    wrote:
    > 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
    Emanuele D'Arrigo, Oct 31, 2008
    #4
  5. On Oct 31, 4:38 am, Rafe <> wrote:
    > 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
    Emanuele D'Arrigo, Oct 31, 2008
    #5
  6. 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
    Emanuele D'Arrigo, Oct 31, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,107
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,081
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    463
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    170
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    646
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page