在Python中传递具有多个返回值作为参数的函数

时间:2022-09-25 15:55:49

So, Python functions can return multiple values. It struck me that it would be convenient (though a bit less readable) if the following were possible.

因此,Python函数可以返回多个值。让我感到震惊的是,如果可能的话,这将是方便的(尽管可读性稍差)。

a = [[1,2],[3,4]]

def cord():
    return 1, 1

def printa(y,x):
    print a[y][x]

printa(cord())

...but it's not. I'm aware that you can do the same thing by dumping both return values into temporary variables, but it doesn't seem as elegant. I could also rewrite the last line as "printa(cord()[0], cord()[1])", but that would execute cord() twice.

......但事实并非如此。我知道你可以通过将两个返回值转储到临时变量中来做同样的事情,但它看起来并不优雅。我还可以将最后一行重写为“printa(cord()[0],cord()[1])”,但这会执行两次cord()。

Is there an elegant, efficient way to do this? Or should I just see that quote about premature optimization and forget about this?

有一种优雅,有效的方法吗?或者我应该只看到关于过早优化的引用并忘记这一点?

3 个解决方案

#1


printa(*cord())

The * here is an argument expansion operator... well I forget what it's technically called, but in this context it takes a list or tuple and expands it out so the function sees each list/tuple element as a separate argument.

*这里是一个参数扩展运算符......我忘记它在技术上被称为什么,但在这个上下文中它需要一个列表或元组并将其展开,以便函数将每个列表/元组元素看作一个单独的参数。

It's basically the reverse of the * you might use to capture all non-keyword arguments in a function definition:

它基本上与您可能用于捕获函数定义中的所有非关键字参数的相反:

def fn(*args):
    # args is now a tuple of the non-keyworded arguments
    print args

fn(1, 2, 3, 4, 5)

prints (1, 2, 3, 4, 5)

打印(1,2,3,4,5)

fn(*[1, 2, 3, 4, 5])

does the same.

做同样的事。

#2


Try this:

>>> def cord():
...     return (1, 1)
...
>>> def printa(y, x):
...     print a[y][x]
...
>>> a=[[1,2],[3,4]]
>>> printa(*cord())
4

The star basically says "use the elements of this collection as positional arguments." You can do the same with a dict for keyword arguments using two stars:

这位明星基本上说“使用这个集合的元素作为位置参数。”您可以使用两个星形对关键字参数的dict执行相同的操作:

>>> a = {'a' : 2, 'b' : 3}
>>> def foo(a, b):
...    print a, b
...
>>> foo(**a)
2 3

#3


Actually, Python doesn't really return multiple values, it returns one value which can be multiple values packed into a tuple. Which means that you need to "unpack" the returned value in order to have multiples. A statement like

实际上,Python并没有真正返回多个值,它返回一个值,可以将多个值打包到元组中。这意味着您需要“解包”返回的值才能获得倍数。像这样的陈述

x,y = cord()

does that, but directly using the return value as you did in

这样做,但直接使用你所做的返回值

printa(cord())

doesn't, that's why you need to use the asterisk. Perhaps a nice term for it might be "implicit tuple unpacking" or "tuple unpacking without assignment".

没有,这就是你需要使用星号的原因。也许一个很好的术语可能是“隐式元组解包”或“没有赋值的元组解包”。

#1


printa(*cord())

The * here is an argument expansion operator... well I forget what it's technically called, but in this context it takes a list or tuple and expands it out so the function sees each list/tuple element as a separate argument.

*这里是一个参数扩展运算符......我忘记它在技术上被称为什么,但在这个上下文中它需要一个列表或元组并将其展开,以便函数将每个列表/元组元素看作一个单独的参数。

It's basically the reverse of the * you might use to capture all non-keyword arguments in a function definition:

它基本上与您可能用于捕获函数定义中的所有非关键字参数的相反:

def fn(*args):
    # args is now a tuple of the non-keyworded arguments
    print args

fn(1, 2, 3, 4, 5)

prints (1, 2, 3, 4, 5)

打印(1,2,3,4,5)

fn(*[1, 2, 3, 4, 5])

does the same.

做同样的事。

#2


Try this:

>>> def cord():
...     return (1, 1)
...
>>> def printa(y, x):
...     print a[y][x]
...
>>> a=[[1,2],[3,4]]
>>> printa(*cord())
4

The star basically says "use the elements of this collection as positional arguments." You can do the same with a dict for keyword arguments using two stars:

这位明星基本上说“使用这个集合的元素作为位置参数。”您可以使用两个星形对关键字参数的dict执行相同的操作:

>>> a = {'a' : 2, 'b' : 3}
>>> def foo(a, b):
...    print a, b
...
>>> foo(**a)
2 3

#3


Actually, Python doesn't really return multiple values, it returns one value which can be multiple values packed into a tuple. Which means that you need to "unpack" the returned value in order to have multiples. A statement like

实际上,Python并没有真正返回多个值,它返回一个值,可以将多个值打包到元组中。这意味着您需要“解包”返回的值才能获得倍数。像这样的陈述

x,y = cord()

does that, but directly using the return value as you did in

这样做,但直接使用你所做的返回值

printa(cord())

doesn't, that's why you need to use the asterisk. Perhaps a nice term for it might be "implicit tuple unpacking" or "tuple unpacking without assignment".

没有,这就是你需要使用星号的原因。也许一个很好的术语可能是“隐式元组解包”或“没有赋值的元组解包”。