function expression with 2 arguments

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

    #1

    function expression with 2 arguments

    is there a way to write a expression of a function with more than 1
    argument?

    e.g., i want a expression that's equivalent to

    def f(x,y)
    return x+y

    Xah
    xah@xahlee.org


  • Harlin Seritt

    #2
    Re: function expression with 2 arguments

    Not exactly sure what you're looking for but you can do the following:

    def dosomething(num list):
    return numlist[0] + numlist[1]

    numlist = [ 5, 10]
    val = dosomething(num list)

    If so, that would be somewhat pointless. It's always best to keep it
    simple. It looks like the function you wrote above is very adequate for
    the results you want returned.

    Comment

    • Reinhold Birkenfeld

      #3
      Re: function expression with 2 arguments

      Xah Lee wrote:[color=blue]
      > is there a way to write a expression of a function with more than 1
      > argument?
      >
      > e.g., i want a expression that's equivalent to
      >
      > def f(x,y)
      > return x+y[/color]

      Looking for lambda?

      Reinhold

      Comment

      • Peter Hansen

        #4
        Re: function expression with 2 arguments

        Xah Lee wrote:[color=blue]
        > is there a way to write a expression of a function with more than 1
        > argument?
        >
        > e.g., i want a expression that's equivalent to
        >
        > def f(x,y)
        > return x+y[/color]

        Since assignment is a statement in Python, not an expression,
        and since "def f" is an assignment that binds a function
        object to the name "f", you can't do exactly what you've
        asked for.

        On the other hand, this should be about equivalent, though
        it's not merely an expression:

        f = lambda x, y: x + y

        Comment

        • Xah Lee

          #5
          Re: function expression with 2 arguments

          lambda x, y: x + y

          that's what i was looking for.

          .... once i have a lambda expr, how to apply it to arguments?

          e.g. in Mathematica
          Function[#1+#2][a,b]

          Python doc is quite confounded in it's way of organization centered
          around implementation tied to hardware (as most imperative languages
          are hardware-centric), as opposed to algorithm math concepts.

          Xah
          xah@xahlee.org


          Comment

          • Leif K-Brooks

            #6
            Re: function expression with 2 arguments

            Xah Lee wrote:[color=blue]
            > lambda x, y: x + y
            >
            > that's what i was looking for.
            >
            > ... once i have a lambda expr, how to apply it to arguments?[/color]

            The official home of the Python Programming Language

            Comment

            • Xah Lee

              #7
              Re: function expression with 2 arguments

              once i have a expresson of a function, how to apply it to arguments?

              e.g. if i have
              lambda x,y:x+y
              i have to applied it to a,b in my code.

              Xah
              xah@xahlee.org


              Comment

              • Roel Schroeven

                #8
                Re: function expression with 2 arguments

                Xah Lee wrote:[color=blue]
                > once i have a expresson of a function, how to apply it to arguments?
                >
                > e.g. if i have
                > lambda x,y:x+y
                > i have to applied it to a,b in my code.[/color]

                OK, I'll bite.

                As with any other callable, you can simply call it like this:

                a = 4
                b = 24
                (lambda x, y: x+y)(a, b)

                Of course, you could just as well simply write

                a+b

                instead.

                Most often the lambda is not used directly, but passed to a function. A
                trivial example:

                def f(fn, a, b):
                return fn(a, b)

                f(lambda x, y: x+y, 3, 42)

                --
                "Codito ergo sum"
                Roel Schroeven

                Comment

                • Xah Lee

                  #9
                  Re: function expression with 2 arguments


                  Roel Schroeven wrote:[color=blue]
                  > (lambda x, y: x+y)(a, b)[/color]

                  Thanks. That's what i was looking for.

                  where in Pytho doc can one find this? or the lambda with multiple
                  params?

                  [color=blue]
                  > Most often the lambda is not used directly, but passed to a function.[/color]

                  That is because the IT morons has been throughly brainwashed by
                  imperative shits. (and these morons don't even know it)

                  i'll explain the ins and outs of expressions of functions some other
                  day.

                  Xah
                  xah@xahlee.org


                  Comment

                  • Peter Hansen

                    #10
                    Re: function expression with 2 arguments

                    Xah Lee wrote:[color=blue]
                    > Roel Schroeven wrote:
                    >[color=green]
                    >>(lambda x, y: x+y)(a, b)[/color]
                    >
                    > Thanks. That's what i was looking for.
                    >
                    > where in Pytho doc can one find this? or the lambda with multiple
                    > params?
                    >[color=green]
                    >>Most often the lambda is not used directly, but passed to a function.[/color]
                    >
                    > That is because the IT morons has been throughly brainwashed by
                    > imperative shits. (and these morons don't even know it)
                    >
                    > i'll explain the ins and outs of expressions of functions some other
                    > day.[/color]

                    After you read about them, I guess? We look forward to your
                    profound words on the subject.

                    Sheesh...

                    Comment

                    • Xah Lee

                      #11
                      Re: function expression with 2 arguments

                      PS sorry for the rude remarks out of nowhere.

                      Xah

                      Comment

                      • Steve Holden

                        #12
                        Re: function expression with 2 arguments

                        Xah Lee wrote:[color=blue]
                        > PS sorry for the rude remarks out of nowhere.
                        >
                        > Xah
                        >[/color]
                        Wow, signs of developing inter-personal skills. I must assume that
                        c.l.py is having its benign influence on you too!

                        regards
                        Steve
                        --
                        Meet the Python developers and your c.l.py favorites March 23-25
                        Come to PyCon DC 2005 http://www.pycon.org/
                        Steve Holden http://www.holdenweb.com/

                        Comment

                        • Xah Lee

                          #13
                          Re: function expression with 2 arguments



                          if i understand correctly, forms such as
                          (lambda x,y:x+y)(a,b)
                          can only be gained thru experience? and not documented directly
                          anywhere in the official docs?

                          Xah
                          xah@xahlee.org


                          Comment

                          • Leif K-Brooks

                            #14
                            Re: function expression with 2 arguments

                            Xah Lee wrote:[color=blue]
                            > if i understand correctly, forms such as
                            > (lambda x,y:x+y)(a,b)
                            > can only be gained thru experience? and not documented directly
                            > anywhere in the official docs?[/color]

                            The official documentation can't list every possible permutation of the
                            various syntactic constructs. It does explain parenthesis, the lambda
                            expression, and calling syntax; the particular combination of the three
                            that you're using can therefor be logically understood from the docs.

                            Comment

                            • Simon Percivall

                              #15
                              Re: function expression with 2 arguments

                              Actually, lambda forms are quite precisely documented at
                              http://docs.python.org/ref/lambdas.html if you feel than reading
                              the tutorial (specifically http://docs.python.org/tut/node6.html
                              section 4.7.5) is too base for you.

                              Comment

                              Working...