function expression with 2 arguments

H

Harlin Seritt

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

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

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

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.
 
R

Reinhold Birkenfeld

Xah said:
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

Looking for lambda?

Reinhold
 
P

Peter Hansen

Xah said:
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

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
 
X

Xah Lee

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
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html
 
N

Nick Coghlan

Xah said:
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.

Actually, Python's docs are centred around the fact that they expect people to
start out by at least skimming the freaking tutorial. . .

Cheers,
Nick.
 
R

Roel Schroeven

Xah said:
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)
 
X

Xah Lee

Roel said:
(lambda x, y: x+y)(a, b)

Thanks. That's what i was looking for.

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

Most often the lambda is not used directly, but passed to a function.

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
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html
 
P

Peter Hansen

Xah said:
Thanks. That's what i was looking for.

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


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.

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

Sheesh...
 
S

Steve Holden

Xah said:
PS sorry for the rude remarks out of nowhere.

Xah
Wow, signs of developing inter-personal skills. I must assume that
c.l.py is having its benign influence on you too!

regards
Steve
 
L

Leif K-Brooks

Xah said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top