简体   繁体   English

函数返回返回int或int列表。 将第一个与变量相关联

[英]Function returns returns either int or list of ints. Assinging the first to a variable

Suppose we have 假设我们有

def f_1():
    return (3, 4)

def f_2():
    return 2

The functions are given. 给出了功能。 It is not code that I can modify. 我无法修改的代码。 We know they return either an integer or a sequence of them. 我们知道它们返回的是整数或序列。

I would like to assign the return to a variable, where the variable should take only the first of the integers if the return is a sequence of them. 我想将return分配给一个变量,如果return是它们的序列,则该变量应仅取整数的第一个。

Is there a build-in syntactic device in Python that allows me to do this? Python中是否有内置语法设备可让我执行此操作?

for function in [f_1, f_2]:
    ...
    x = function()[0] # Works for f_1, breaks for f_2
    y = function()    # Works for f_2, assigns (3, 4) for f_1 while 
                      #   we really would like only the 3 to be the 
                      #   assigned value

Note: Assume that we don't know which of the functions return the sequence and which return just a number. 注意:假设我们不知道哪个函数返回序列,哪个函数仅返回数字。

Is there a build-in syntactic device 有内置的句法设备吗

No. The usual practice is to ask for forgivness : 不,通常的做法是请求 宽恕

x = function()
try:
    x = x[0]
except TypeError:
    pass

I wouldnt go for try-except statement. 我不会去尝试-除外声明。 To me it's an overkill. 对我来说,这太过分了。

More likely I would try to do something like: 我更有可能尝试执行以下操作:

result = function()
# this can be changed to abc.Sequence for more general approach
if isinstance(result, (list, tuple)): 
    result = result[0]

Going in the other direction, wrap f_2 in another function to ensure that the function called in the body of the loop always returns a sequence. 在另一个方向上,将f_2包装在另一个函数中,以确保在循环主体中调用的函数始终返回序列。

for function in [f_1, lambda : (f_2(),)]:
    x = function()[0]

The additional overhead of another function call may make this approach undesirable. 另一个函数调用的额外开销可能会使这种方法不受欢迎。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化整数列表。 解释器返回“ValueError: invalid litteral for int()” - Initializing list of ints. Interpreter returns “ValueError: invalid litteral for int()” 是否有python函数返回列表中未出现的第一个正整数? - Is there a python function that returns the first positive int that does not occur in list? 1列具有整数。 另一个有一个整数列表。 如何将数据帧转换为这些对的numpy rec数组? - 1 column has an int. Another has a list of ints. How to convert dataframe into a numpy rec array of these pairs? 将一个数字拆分为一串单独的整数。 'int' object 不可迭代? - Splitting a number into a string of separate ints. 'int' object is not iterable? 在numpy数组的每一行中,我都有一个int和一个Python int列表。 如何在不使用熊猫的情况下将此列表转换为numpy int数组? - In each row of a numpy array, I have an int, and a python list of ints. How do I convert this list into a numpy int array, without using pandas? Function 只返回列表的第一个元素 - Function returns only first element of the list 按每个整数的第一位数对整数列表进行排序 - Sort list of ints by the first digit of each int 返回列表的正面或负面元素的函数 - Function which returns either the positive or negative elements of the list 接受int或int元组作为python函数参数 - Accept either int or tuple of ints as a python function parameter 赋值给一个变量 - Assinging to a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM