Exec and Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Emanuele D'Arrigo

    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("myObj ect.myCommand() ")

    Now suppose the method

    def execThis(aComma ndInAString):
    exec(aCommandIn AString)

    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.myComm and() 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
  • James Mills

    #2
    Re: Exec and Scope

    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 <manu3d@gmail.c omwrote:
    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("myObj ect.myCommand() ")
    >
    Now suppose the method
    >
    def execThis(aComma ndInAString):
    exec(aCommandIn AString)
    >
    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.myComm and() 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
    --

    >


    --
    --
    -- "Problems are solved by method"

    Comment

    • Rafe

      #3
      Re: Exec and Scope

      On Oct 31, 10:47 am, "Emanuele D'Arrigo" <man...@gmail.c omwrote:
      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("myObj ect.myCommand() ")
      >
      Now suppose the method
      >
      def execThis(aComma ndInAString):
          exec(aCommandIn AString)
      >
      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.myComm and() 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(myObjec t, "my_command ")
      >>attr()
      hello


      - Rafe

      Comment

      • Emanuele D'Arrigo

        #4
        Re: Exec and Scope

        On Oct 31, 4:38 am, Rafe <rafesa...@gmai l.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



        Comment

        • Emanuele D'Arrigo

          #5
          Re: Exec and Scope

          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(input Method):
          inputMethod()

          myCommand(myMet hod)

          and that works flawlessly! Thank you guys!

          Manu

          Comment

          Working...