How do I create an array of functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven W. Orr

    #1

    How do I create an array of functions?

    I have a table of integers and each time I look up a value from the table
    I want to call a function using the table entry as an index into an array
    whose values are the different functions. I haven't seen anything on how
    to do this in python.

    TIA

    --
    Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
  • Diez B. Roggisch

    #2
    Re: How do I create an array of functions?

    Steven W. Orr schrieb:
    I have a table of integers and each time I look up a value from the
    table I want to call a function using the table entry as an index into
    an array whose values are the different functions. I haven't seen
    anything on how to do this in python.
    def f():
    pass

    fmap = { key: f }


    fmap[key]()


    Diez

    Comment

    • Rob Wolfe

      #3
      Re: How do I create an array of functions?


      Steven W. Orr wrote:
      I have a table of integers and each time I look up a value from the table
      I want to call a function using the table entry as an index into an array
      whose values are the different functions. I haven't seen anything on how
      to do this in python.
      Do you mean something like that?

      # test.py

      def fun1(): return "fun1"
      def fun2(): return "fun2"
      def fun3(): return "fun3"

      # list of functions
      dsp = [f for fname, f in sorted(globals( ).items()) if callable(f)]
      tab = range(len(dsp))
      print dsp[tab[2]]()

      # dictionary of functions
      d = dict([(fname, f) for fname, f in globals().items () if
      callable(f)])
      tab = [fname for fname, f in sorted(globals( ).items()) if callable(f)]
      print d[tab[2]]()

      --
      HTH,
      Rob

      Comment

      • Paul Rubin

        #4
        Re: How do I create an array of functions?

        "Steven W. Orr" <steveo@syslang .netwrites:
        I have a table of integers and each time I look up a value from the
        table I want to call a function using the table entry as an index into
        an array whose values are the different functions. I haven't seen
        anything on how to do this in python.
        func_array = [f1, f2, f3] # array of functions
        index = table_lookup()
        func_array[index](x,y,z) # select a function and call it

        Comment

        • Steven D'Aprano

          #5
          Re: How do I create an array of functions?

          On Mon, 19 Feb 2007 00:16:39 -0800, Rob Wolfe wrote:
          >
          Steven W. Orr wrote:
          >I have a table of integers and each time I look up a value from the table
          >I want to call a function using the table entry as an index into an array
          >whose values are the different functions. I haven't seen anything on how
          >to do this in python.
          >
          Do you mean something like that?
          >
          # test.py
          >
          def fun1(): return "fun1"
          def fun2(): return "fun2"
          def fun3(): return "fun3"
          >
          # list of functions
          dsp = [f for fname, f in sorted(globals( ).items()) if callable(f)]
          Hmmm... when I try that, I get dozens of other functions, not just fun1,
          fun2 and fun3. And not just functions either; I also get classes.

          Does Python have a function that will read my mind and only return the
          objects I'm thinking of?



          --
          Steven.

          Comment

          • Rob Wolfe

            #6
            Re: How do I create an array of functions?


            Steven D'Aprano wrote:
            On Mon, 19 Feb 2007 00:16:39 -0800, Rob Wolfe wrote:
            >

            Steven W. Orr wrote:
            I have a table of integers and each time I look up a value from the table
            I want to call a function using the table entry as an index into an array
            whose values are the different functions. I haven't seen anything on how
            to do this in python.
            Do you mean something like that?

            # test.py

            def fun1(): return "fun1"
            def fun2(): return "fun2"
            def fun3(): return "fun3"

            # list of functions
            dsp = [f for fname, f in sorted(globals( ).items()) if callable(f)]
            >
            Hmmm... when I try that, I get dozens of other functions, not just fun1,
            fun2 and fun3. And not just functions either; I also get classes.
            Oh, really? Where are these _other_ functions and classes
            in *MY* example?
            Does Python have a function that will read my mind and only return the
            objects I'm thinking of?
            Your sarcasm is unnecessary.
            Using of `globals` function was easier to write this example.
            That's all.

            --
            Rob

            Comment

            • John Machin

              #7
              Re: How do I create an array of functions?

              On Feb 19, 11:47 pm, Steven D'Aprano
              <s...@REMOVE.TH IS.cybersource. com.auwrote:
              On Mon, 19 Feb 2007 00:16:39 -0800, Rob Wolfe wrote:
              >
              Steven W. Orr wrote:
              I have a table of integers and each time I look up a value from the table
              I want to call a function using the table entry as an index into an array
              whose values are the different functions. I haven't seen anything on how
              to do this in python.
              >
              Do you mean something like that?
              >
              # test.py
              >
              def fun1(): return "fun1"
              def fun2(): return "fun2"
              def fun3(): return "fun3"
              >
              # list of functions
              dsp = [f for fname, f in sorted(globals( ).items()) if callable(f)]
              >
              Hmmm... when I try that, I get dozens of other functions, not just fun1,
              fun2 and fun3. And not just functions either; I also get classes.
              >
              Does Python have a function that will read my mind and only return the
              objects I'm thinking of?
              Yup.

              Stevens_mind = r"fun[1-3]$"

              After "if callable(f)", put

              and re.match(Steven s_mind, fname)
              and not isinstance(f, type)






              Comment

              • Steven D'Aprano

                #8
                Re: How do I create an array of functions?

                On Mon, 19 Feb 2007 05:17:03 -0800, Rob Wolfe wrote:
                # test.py
                >
                def fun1(): return "fun1"
                def fun2(): return "fun2"
                def fun3(): return "fun3"
                >
                # list of functions
                dsp = [f for fname, f in sorted(globals( ).items()) if callable(f)]
                >>
                >Hmmm... when I try that, I get dozens of other functions, not just fun1,
                >fun2 and fun3. And not just functions either; I also get classes.
                >
                Oh, really? Where are these _other_ functions and classes
                in *MY* example?
                I ran your example, word for word. Copied it and pasted it into my Python
                session.


                >Does Python have a function that will read my mind and only return the
                >objects I'm thinking of?
                >
                Your sarcasm is unnecessary.
                Using of `globals` function was easier to write this example. That's all.
                Actually, it wasn't easier to write at all.

                Your version:
                dsp = [f for fname, f in sorted(globals( ).items()) if callable(f)]

                Sensible version:
                dsp = [fun1, fun2, fun3]

                Not only is your version brittle, but it is also about three times as
                much typing.



                --
                Steven.

                Comment

                Working...