calling variable function name ?

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

    calling variable function name ?

    I don't know the exact terminology in python, but this is something i
    am trying to do

    i have 3 functions lets say
    FA(param1,param 2)
    FB(param1,param 2)
    FC(param1,param 2)

    temp = "B" #something entered by user. now i want to call FB. I don't
    want to do an if else because if have way too many methods like
    this...

    var = "F" + temp
    var(param1, param2)

    This does not work ofcourse. Does anyone know how to implement this ?



  • Arnaud Delobelle

    #2
    Re: calling variable function name ?

    On Apr 8, 8:52 pm, TkNeo <tarun....@gmai l.comwrote:
    I don't know the exact terminology in python, but this is something i
    am trying to do
    >
    i have 3 functions lets say
    FA(param1,param 2)
    FB(param1,param 2)
    FC(param1,param 2)
    >
    temp = "B" #something entered by user. now i want to call FB. I don't
    want to do an if else because if have way too many methods like
    this...
    >
    var = "F" + temp
    var(param1, param2)
    >
    This does not work ofcourse. Does anyone know how to implement this ?
    F = { 'A': FA, 'B':FB, 'C':FC }

    F['A'](param1, param2)

    HTH

    --
    Arnaud

    Comment

    • George Sakkis

      #3
      Re: calling variable function name ?

      On Apr 8, 3:52 pm, TkNeo <tarun....@gmai l.comwrote:
      I don't know the exact terminology in python, but this is something i
      am trying to do
      >
      i have 3 functions lets say
      FA(param1,param 2)
      FB(param1,param 2)
      FC(param1,param 2)
      >
      temp = "B" #something entered by user. now i want to call FB. I don't
      want to do an if else because if have way too many methods like
      this...
      >
      var = "F" + temp
      var(param1, param2)
      Try this:

      func = globals()["F" + temp]
      func(param1, param2)

      HTH,
      George

      Comment

      • TkNeo

        #4
        Re: calling variable function name ?

        On Apr 8, 7:51 pm, George Sakkis <george.sak...@ gmail.comwrote:
        On Apr 8, 3:52 pm,TkNeo<tarun. ...@gmail.comwr ote:
        >
        I don't know the exact terminology in python, but this is something i
        am trying to do
        >
        i have 3 functions lets say
        FA(param1,param 2)
        FB(param1,param 2)
        FC(param1,param 2)
        >
        temp = "B" #something entered by user. now i want to call FB. I don't
        want to do an if else because if have way too many methods like
        this...
        >
        var = "F" + temp
        var(param1, param2)
        >
        Try this:
        >
        func = globals()["F" + temp]
        func(param1, param2)
        >
        HTH,
        George


        George - Thanks for your reply but what you suggested is not working:

        def FA(param1,param 2):
        print "FA" + param1 + " " + param2
        def FA(param1,param 2):
        print "FB" + param1 + " " + param2
        def FA(param1,param 2):
        print "FC" + param1 + " " + param2

        temp = sys.argv[1]

        func = globals()["F" + temp]
        func("Hello", "World")


        I ran the script with first parameter as B and i get the following
        message
        KeyError: 'FB'

        Comment

        • Arnaud Delobelle

          #5
          Re: calling variable function name ?

          TkNeo <tarun.kap@gmai l.comwrites:
          >
          George - Thanks for your reply but what you suggested is not working:
          >
          def FA(param1,param 2):
          print "FA" + param1 + " " + param2
          def FA(param1,param 2):
          print "FB" + param1 + " " + param2
          def FA(param1,param 2):
          print "FC" + param1 + " " + param2
          >
          temp = sys.argv[1]
          >
          func = globals()["F" + temp]
          func("Hello", "World")
          >
          >
          I ran the script with first parameter as B and i get the following
          message
          KeyError: 'FB'
          Perhaps if you call your three function FA, FB, FC instead of FA, FA,
          FA it'll help?

          --
          Arnaud

          Comment

          • thinkofwhy

            #6
            Re: calling variable function name ?

            Try a dictionary:

            def funcA(blah, blah)
            def funcB(blah, blah)
            def funcC(blah, blah)
            functions = {'A': funcA, 'B': funcB, 'C':
            funcC}
            user_func = 'A'
            functions[user_func] #execute function


            "Arnaud Delobelle" <arnodel@google mail.comwrote
            in message news:m28wyvba54 .fsf@googlemail .com...
            TkNeo <tarun.kap@gmai l.comwrites:
            >
            >>
            >George - Thanks for your reply but what you
            >suggested is not working:
            >>
            >def FA(param1,param 2):
            > print "FA" + param1 + " " + param2
            >def FA(param1,param 2):
            > print "FB" + param1 + " " + param2
            >def FA(param1,param 2):
            > print "FC" + param1 + " " + param2
            >>
            >temp = sys.argv[1]
            >>
            >func = globals()["F" + temp]
            >func("Hello" , "World")
            >>
            >>
            >I ran the script with first parameter as B and
            >i get the following
            >message
            >KeyError: 'FB'
            >
            Perhaps if you call your three function FA, FB,
            FC instead of FA, FA,
            FA it'll help?
            >
            --
            Arnaud

            Comment

            • Duncan Booth

              #7
              Re: calling variable function name ?

              "thinkofwhy " <thinkofwhy@yah oo.cawrote:
              Try a dictionary:
              >
              def funcA(blah, blah)
              def funcB(blah, blah)
              def funcC(blah, blah)
              functions = {'A': funcA, 'B': funcB, 'C':
              funcC}
              user_func = 'A'
              functions[user_func] #execute function
              Python has a neat concept for making this easy :^) it is called a class.

              class MyFunctions(obj ect):
              def funcA(self, param1, param2):
              print "FA " + param1 + " " + param2

              def funcB(self, param1, param2):
              print "FB " + param1 + " " + param2

              def funcC(self, param1, param2):
              print "FC " + param1 + " " + param2

              def defaultFunc(sel f, *args):
              print "Command not recognised"

              def doCommand(self, cmd, *args):
              return getattr(self, 'func'+cmd, self.defaultFun c)(*args)

              functions = MyFunctions()
              result = functions.doCom mand('A', 'foo', 'bar')

              You have to add an extra 'self' argument to each function (or make it a
              staticmethod), but you could always use it to store state without resorting
              to globals.

              Comment

              • TkNeo

                #8
                Re: calling variable function name ?

                On Apr 30, 11:05 am, Arnaud Delobelle <arno...@google mail.comwrote:
                TkNeo<tarun.... @gmail.comwrite s:
                >
                George - Thanks for your reply but what you suggested is not working:
                >
                def FA(param1,param 2):
                print "FA" + param1 + " " + param2
                def FA(param1,param 2):
                print "FB" + param1 + " " + param2
                def FA(param1,param 2):
                print "FC" + param1 + " " + param2
                >
                temp = sys.argv[1]
                >
                func = globals()["F" + temp]
                func("Hello", "World")
                >
                I ran the script with first parameter as B and i get the following
                message
                KeyError: 'FB'
                >
                Perhaps if you call your three function FA, FB, FC instead of FA, FA,
                FA it'll help?
                >
                --
                Arnaud
                oh crap. I can't believe I was doing that ! :) Thanks for pointing
                out. works perfect.

                Comment

                Working...