Executing a list of functions

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

    #1

    Executing a list of functions


    Seems to me that one should be able to put the names of several
    functions in a list and then have the list executed. But it seems the
    output of the functions is hidden, only their return value is visible.
    Is this because the list execution is another scope?

    Thanx,

    jh

    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~

    def a():
    print "this is a"

    def b():
    print "this is b"

    lst = [a(), b()]

    lst

  • 7stud

    #2
    Re: Executing a list of functions

    lst = [a, b]

    The () symbol causes the named function to execute, and a function
    call in the code is always replaced by the function's return value.

    Comment

    • 7stud

      #3
      Re: Executing a list of functions

      On Mar 16, 3:59 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
      lst = [a, b]
      >
      The () symbol causes the named function to execute, and a function
      call in the code is always replaced by the function's return value.
      Try this:

      ------
      def a():
      print "this is a"

      def b():
      print "this is b"

      lst = [a(), b()]
      ------

      To create the list, the terms inside the list have to be evaluated.
      That causes a and b to execute, and the function calls are replaced by
      the each function's return value. Since your functions don't have a
      return statement, the value None is returned. To see that, add this
      line:

      print lst


      Comment

      • Michel Claveau

        #4
        Re: Executing a list of functions

        Hi!

        Your code run OK for me.

        But, if you want "time-lag" (sorry for my english) execution, you can
        try this:


        def a():
        print "this is a"

        def b():
        print "this is b"

        lst = [a, b]
        [f() for f in lst]







        --
        @-salutations

        Michel Claveau


        Comment

        • James Stroud

          #5
          Re: Executing a list of functions

          HMS Surprise wrote:
          Seems to me that one should be able to put the names of several
          functions in a list and then have the list executed. But it seems the
          output of the functions is hidden, only their return value is visible.
          Is this because the list execution is another scope?
          >
          Thanx,
          >
          jh
          >
          ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
          >
          def a():
          print "this is a"
          >
          def b():
          print "this is b"
          >
          lst = [a(), b()]
          >
          lst
          >
          The "print" statement does nothing to return a value from the function,
          so the strings "this is *" will not be stored in your list. They will,
          however, be printed if you are some how observing your program output
          (e.g. running it in IDLE, or a command shell).

          To save the "results" of the functions, you need to produce results,
          which means actually using "return" to return some value. Here is an
          example:


          def a():
          print "this is a"
          return "return value from a"

          def b():
          print "this is b"
          return "return value from b"

          functions = [a, b]
          results = [f() for f in functions]
          print results


          Here is the result of this example:


          pydef a():
          .... print "this is a"
          .... return "return value from a"
          ....
          pydef b():
          .... print "this is b"
          .... return "return value from b"
          ....
          pyfunctions = [a, b]
          pyresults = [f() for f in functions]
          this is a
          this is b
          pyprint results
          ['return value from a', 'return value from b']


          A fun, but unfortunately deprecated, way to do this is with the "apply"
          function in conjunction with the "map" function:


          def a():
          print "this is a"
          return "return value from a"

          def b():
          print "this is b"
          return "return value from b"

          functions = [a, b]
          results = map(apply, functions)
          print results


          Here is this example at work:


          pydef a():
          .... print "this is a"
          .... return "return value from a"
          ....
          pydef b():
          .... print "this is b"
          .... return "return value from b"
          ....
          pyfunctions = [a, b]
          pyresults = map(apply, functions)
          this is a
          this is b
          pyprint results
          ['return value from a', 'return value from b']


          James

          Comment

          • HMS Surprise

            #6
            Re: Executing a list of functions

            On Mar 16, 6:44 pm, James Stroud <jstr...@mbi.uc la.eduwrote:
            HMS Surprise wrote:
            Seems to me that one should be able to put the names of several
            functions in a list and then have the list executed. But it seems the
            output of the functions is hidden, only their return value is visible.
            Is this because the list execution is another scope?
            >
            Thanx,
            >
            jh
            >
            ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
            >
            def a():
            print "this is a"
            >
            def b():
            print "this is b"
            >
            lst = [a(), b()]
            >
            lst
            >
            The "print" statement does nothing to return a value from the function,
            so the strings "this is *" will not be stored in your list. They will,
            however, be printed if you are some how observing your program output
            (e.g. running it in IDLE, or a command shell).
            >
            To save the "results" of the functions, you need to produce results,
            which means actually using "return" to return some value. Here is an
            example:
            >
            def a():
            print "this is a"
            return "return value from a"
            >
            def b():
            print "this is b"
            return "return value from b"
            >
            functions = [a, b]
            results = [f() for f in functions]
            print results
            >
            Here is the result of this example:
            >
            pydef a():
            ... print "this is a"
            ... return "return value from a"
            ...
            pydef b():
            ... print "this is b"
            ... return "return value from b"
            ...
            pyfunctions = [a, b]
            pyresults = [f() for f in functions]
            this is a
            this is b
            pyprint results
            ['return value from a', 'return value from b']
            >
            A fun, but unfortunately deprecated, way to do this is with the "apply"
            function in conjunction with the "map" function:
            >
            def a():
            print "this is a"
            return "return value from a"
            >
            def b():
            print "this is b"
            return "return value from b"
            >
            functions = [a, b]
            results = map(apply, functions)
            print results
            >
            Here is this example at work:
            >
            pydef a():
            ... print "this is a"
            ... return "return value from a"
            ...
            pydef b():
            ... print "this is b"
            ... return "return value from b"
            ...
            pyfunctions = [a, b]
            pyresults = map(apply, functions)
            this is a
            this is b
            pyprint results
            ['return value from a', 'return value from b']
            >
            James
            Thanks to all for posting.

            Why is apply deprecated?

            jh

            Comment

            • Alex Martelli

              #7
              Re: Executing a list of functions

              HMS Surprise <john@datavoice int.comwrote:
              ...
              Why is apply deprecated?
              Because it does exacly the same job as just calling the function with
              *a/**k, and there should preferably be only one obvious way to perform a
              given task (this guiding principle leads to simplicity in the language,
              and is common to Python and to the "Spirit of C" as explained in the
              preface of the ISO C Standard -- they phrase it as "offer only one way
              to perform an operation", I believe).


              Alex

              Comment

              Working...