x, temp = func()

Mat
发布于 2009-01-11
11 个回答
0 人赞同
你可以用x = func()[0]来返回第一个值,x = func()[1]来返回第二个,以此类推。
如果你想一次获得多个值,请使用类似x, y = func()[2:4]的东西。
0 人赞同
一个常见的惯例是用"_"作为你希望忽略的元组元素的变量名。比如说。
def f():
return 1, 2, 3
_, _, x = f()
0 人赞同
如果你使用的是 Python 3,你可以在一个变量前使用星号 (在一个赋值的左边),让它在解包时成为一个列表。
# Example 1: a is 1 and b is [2, 3]
a, *b = [1, 2, 3]
# Example 2: a is 1, b is [2, 3], and c is 4
a, *b, c = [1, 2, 3, 4]
# Example 3: b is [1, 2] and c is 3
*b, c = [1, 2, 3]
# Example 4: a is 1 and b is []
a, *b = [1]
0 人赞同
通常的做法是使用虚拟变量_(单下划线),之前很多人在这里表示过。
然而,为了避免与该变量名称的其他用途发生冲突(见this响应),用__(双下划线)代替作为弃用变量可能是更好的做法,如图所示ncoghlan. E.g.:
x, __ = func()
0 人赞同
记住,当你返回一个以上的项目时,你实际上是返回一个元组。所以你可以这样做。
def func():
return 1, 2
print func()[0] # prints 1
print func()[1] # prints 2
0 人赞同
Three simple choices.
Obvious
x, _ = func()
x, junk = func()
Hideous
x = func()[0]
而且有办法用装修公司来做这件事。
def val0( aFunc ):
def pick0( *args, **kw ):
return aFunc(*args,**kw)[0]
return pick0
func0= val0(func)
0 人赞同
最好的解决办法可能是为事物命名,而不是返回无意义的图元(除非在返回的项目的顺序背后有一些逻辑)。例如,你可以使用一个字典。
def func():
return {'lat': 1, 'lng': 2}
latitude = func()['lat']
You could even use 命名元组如果你想添加关于你要返回的东西的额外信息(它不仅仅是一个字典,它是一对坐标)。
from collections import namedtuple
Coordinates = namedtuple('Coordinates', ['lat', 'lng'])
def func():
return Coordinates(lat=1, lng=2)
latitude = func().lat
如果你的 dictionary/tuple 中的对象是紧密联系在一起的,那么为它定义一个类可能是个好主意。这样,你就可以定义更复杂的操作。随之而来的一个自然问题是。我应该在什么时候使用 Python 中的类?
大多数最新版本的python(≥3.7)都有数据类目你可以用非常少的几行代码来定义类。
from dataclasses import dataclass
@dataclass
class Coordinates:
lat: float = 0
lng: float = 0
def func():
return Coordinates(lat=1, lng=2)
latitude = func().lat
The primary advantage of 数据类目 over 命名元组 is that its easier to extend, but there are other differences. Note that by default, 数据类目 are mutable, but you can use @dataclass(frozen=True) instead of @dataclass to force them being immutable.
这里有一段视频这可能有助于你为你的用例挑选正确的数据类。
0 人赞同
This seems like the best choice to me:
val1, val2, ignored1, ignored2 = some_function()
它并不隐晦或丑陋(像func()[index]方法),而且清楚地说明了你的目的。
0 人赞同
如果这是一个你一直在使用的函数,但总是丢弃第二个参数,我认为用lambda为这个函数创建一个没有第二个返回值的别名,会比较不乱。
def func():
return 1, 2
func_ = lambda: func()[0]
func_() # Prints 1
0 人赞同
这不是对该问题的直接回答。相反,它回答了这个问题。"我如何从许多可能的选项中选择一个特定的函数输出?"。
如果你能够编写这个函数(即它不在你不能修改的库中),那么添加一个输入参数,表明你想从这个函数中得到什么。让它成为一个有默认值的命名参数,这样在 "普通情况 "下,你甚至不需要指定它。
def fancy_function( arg1, arg2, return_type=1 ):
ret_val = None
if( 1 == return_type ):
ret_val = arg1 + arg2
elif( 2 == return_type ):
ret_val = [ arg1, arg2, arg1 * arg2 ]
else:
ret_val = ( arg1, arg2, arg1 + arg2, arg1 * arg2 )
return( ret_val )
这种方法给函数提供了关于所需输出的 "预先警告"。因此,它可以跳过不需要的处理,只做获得所需输出的必要工作。同样因为Python是动态类型的,所以返回类型可以改变。注意这个例子是如何返回一个标量、一个列表或一个元组的......任何你喜欢的东西!
0 人赞同
当你有很多函数的输出,而你又不想多次调用它时,我认为选择结果的最清楚的方法是:
results = fct()
a,b = [results[i] for i in list_of_index]
作为一个最起码的工作例子,也证明了函数只被调用一次。
def fct(a):
b=a*2
c=a+2
d=a+b
e=b*2
f=a*a
print("fct called")
return[a,b,c,d,e,f]
results=fct(3)
> fct called
x,y = [results[i] for i in [1,4]]
而这些数值与预期的一样。
results
> [3,6,5,9,12,9]
为了方便,也可以使用Python列表索引。