函数表达式有2个参数 [英] function expression with 2 arguments

查看:68
本文介绍了函数表达式有2个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用超过1

参数写一个函数的表达式?


例如,我想要一个表达式''相当于


def f(x,y)

返回x + y


Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

解决方案

不完全确定你在寻找什么,但你可以做以下事情:


def dosomething(numlist):

返回numlist [0] + numlist [1]


numlist = [5,10]

val = dosomething(numlist)


如果所以,那将是毫无意义的。保持它总是最好的

简单。看起来你上面写的功能非常适合你希望返回的结果。


Xah Lee写道:< blockquote class =post_quotes>有没有办法写一个函数的表达式超过1
参数?

例如,我想要一个等于
def f(x,y)
返回x + y




寻找lambda?


Reinhold


Xah Lee写道:

有没有办法写一个函数的表达式超过1
争论?

例如,我想要一个表达式相当于

def f(x,y)
返回x + y




因为赋值是Python中的一个语句,而不是表达式,

和def f是一个将函数

对象绑定到名称f的赋值,你不能完全按照你要求的b $ b要求。


另一方面,这应该是相当的,但是

它不仅仅是一个表达式:


f = lambda x,y:x + y


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

解决方案

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.


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


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


这篇关于函数表达式有2个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆