function expression with 2 arguments

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Wed Mar 2 06:30:40 EST 2005


Xah Lee wrote:
> 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.

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



More information about the Python-list mailing list