在Python中,可以执行以下操作并访问所需的函数输出:
getNthOutput = myFunc(args)[0] #Will get you the first output of a multi-output function in Python一个人怎么能在卢阿做同样的事情?下面是我的尝试,它给了我一个错误:
getNthOutput = myFunc(args)[1] --Get me the first output of a multi-output function in Lua发布于 2016-04-06 00:19:59
如果您只需要第一个返回值(如您的示例所示),则可以这样做:
first = myFunc(args)如果需要任意返回值,可以使用表构造函数:
function myFunc()
return 1, 2, 'a', 'b'
end
first = ({myFunc()})[1]
print(first)
# 1
n = 4
nth = ({myFunc()})[n]
print(nth)
# b发布于 2016-04-06 08:20:56
您将得到一个错误,因为多个返回值没有作为表返回。因此,您不能使用[]访问任何表成员。
较新的Lua版本提供了一个函数,可以将返回值安全地放入表中,以便以后可以使用索引。
local retVals = table.pack(foo())
local firstValue = retVals[1]或者简单的
table.pack(foo())[1]在旧的Lua版本中,没有函数table.pack,但是您可以使用vararg函数自己实现一个函数。
function myPack(...)
return {...} -- this only works since Lua 5.1
end我不指望你能和5.1岁以上的人一起工作。但请注意,vararg函数的工作方式不同。请参阅关于函数定义的相应Lua引用。
https://stackoverflow.com/questions/36439093
复制相似问题