dynamic func. call

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aljosa Mohorovic

    dynamic func. call

    can i do something like this:

    s = "myFunction "
    a = s() # equals to: a = myFunction()


  • Ola Natvig

    #2
    Re: dynamic func. call

    Aljosa Mohorovic wrote:[color=blue]
    > can i do something like this:
    >
    > s = "myFunction "
    > a = s() # equals to: a = myFunction()
    >
    >[/color]

    a = locals()[x]()

    locals() returns a dictionary with string keys that you can use

    --
    --------------------------------------
    Ola Natvig <ola.natvig@inf osense.no>
    infoSense AS / development

    Comment

    • Tim Jarman

      #3
      Re: dynamic func. call

      Aljosa Mohorovic wrote:
      [color=blue]
      > can i do something like this:
      >
      > s = "myFunction "
      > a = s() # equals to: a = myFunction()[/color]

      Functions are first-class objects in Python, so you can do:

      def myFunction():
      # whatever

      which creates a function object and binds the name myFunction to it. Then:

      s = myFunction

      just binds the name s to your function object, and therefore:

      a = s()

      is the same as:

      a = myFunction()


      --
      Website: www DOT jarmania FULLSTOP com

      Comment

      • ajikoe@gmail.com

        #4
        Re: dynamic func. call

        Try this:

        def myfunc():
        print "helo"

        s = "myfunc()"
        a = eval(s)

        Comment

        • Duncan Booth

          #5
          Re: dynamic func. call

          ajikoe@gmail.co m wrote:
          [color=blue]
          > Try this:
          >
          > def myfunc():
          > print "helo"
          >
          > s = "myfunc()"
          > a = eval(s)
          >[/color]

          No, please don't try that. Good uses for eval are *very* rare, and this
          isn't one of them.

          Use the 'a = locals()[x]()' suggestion (or vars() instead of locals()), or
          even better put all the functions callable by this method into a class and
          use getattr() on an instance of the class.

          A Pythonic way to do this sort of thing is to put all the functions that
          are callable indirectly into a class and give them names which contain a
          prefix to make it obvious that they are callable in this way and then add
          the prefix onto the string:

          class C:
          def command_myfunc( self):
          return 42

          def default_command (self):
          raise NotImplementedE rror('Unknown command')

          def execute(self, s):
          return getattr(self, 'command_'+s, self.default_co mmand)()

          commands = C()
          print commands.execut e('myfunc')

          That way you can be quickly tell which functions can be called indirectly
          and which can't; you can control what happens when no suitable function
          exists; and you can easily extend the functionality by subclassing your
          base class.

          Comment

          Working...