473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
14 1332
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.

Jul 18 '05 #2
Xah Lee wrote:
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
Jul 18 '05 #3
Xah Lee wrote:
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
Jul 18 '05 #4
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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #5
Xah Lee wrote:
lambda x, y: x + y

that's what i was looking for.

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


http://python.org/doc/current/ref/calls.html
Jul 18 '05 #6
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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #7
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
Jul 18 '05 #8

Roel Schroeven wrote:
(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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #9
Xah Lee wrote:
Roel Schroeven wrote:
(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.


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

Sheesh...
Jul 18 '05 #10
PS sorry for the rude remarks out of nowhere.

Xah

Jul 18 '05 #11
Xah Lee wrote:
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
--
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/
Jul 18 '05 #12


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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #13
Xah Lee wrote:
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.
Jul 18 '05 #14
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.

Jul 18 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
11
by: Wayne Cressman | last post by:
I'm writing a function to dynamically change a form validation script depending upon the user's choices. The form onsubmit is: onsubmit="writevalidate(this.select.value);return...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
11
by: 313 Games | last post by:
hi, ive got a question. im making an chat program with server and client using vb 6. but now my problem is, i want to set a topic in each server. i want to send the data from the server to all...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.