简体   繁体   English

Python - 使用元组返回多个值(AWS lambda 函数)

[英]Python - return multiple values using tuple (AWS lambda function)

This is the current code that I have:这是我拥有的当前代码:

def handler():
    ec2 = boto3.resource("ec2", region_name="ap-southeast-2")
    instances = ec2.instances.filter(Filters=[{'Name':'iam-instance-profile.arn', 'Values': ['arn:aws:iam::123456789012:instance-profile/TestRole']}])
    for instance in instances:
        return (instance.private_ip_address)

I'm trying to return all the private_ip_addresses.我正在尝试返回所有 private_ip_addresses。 This code only returns me 1 ip address but I should be getting back 3 addresses.此代码仅返回 1 个 IP 地址,但我应该返回 3 个地址。 If i did a print instead, it returns the correct 3 values如果我改为print ,它会返回正确的 3 个值

I did some research and found using tuple is a good way but it hasn't been working for me thus far.我做了一些研究,发现使用tuple是一种好方法,但到目前为止它一直对我不起作用。 Here is an example code I'm trying out with tuple (that should work with my code if it is correct)这是我正在尝试使用元组的示例代码(如果正确,它应该适用于我的代码)

myList=[1,2,3,4,5,6]

def handler():
   tup=()
   for element in myList:
       tup+= (element,)
   return tup

This currently returns a null value.这当前返回一个空值。 However, if i do print tup , it returns me with (1, 2, 3, 4, 5, 6)但是,如果我print tup ,它会返回(1, 2, 3, 4, 5, 6)

Can anyone let me know where I am going wrong with my use of tuple.任何人都可以让我知道我使用元组的地方出了什么问题。 OR if there is a better way to do this (corresponding with how lambda functions work)?或者是否有更好的方法来做到这一点(对应于 lambda 函数的工作方式)?

The statement " return (instance.private_ip_address) " returns a tuple consisting of a single value.语句“ return (instance.private_ip_address) ”返回一个由单个值组成的元组。 The fact that it is called from a loop, doesn't make any difference - as soon as the return statement is executed during the first iteration, the loop terminates.从循环调用它的事实没有任何区别 - 一旦在第一次迭代期间执行return语句,循环就会终止。

The following should work:以下应该工作:

def handler():
    ec2 = boto3.resource("ec2", region_name="ap-southeast-2")
    instances = ec2.instances.filter(Filters=[{'Name':'iam-instance-profile.arn', 'Values': ['arn:aws:iam::123456789012:instance-profile/TestRole']}])
    return tuple([instance.private_ip_address for instance in instances])

In your handler code, when you use a print instead of a return it works fine as it doesn't break the loop or the function.在您的处理程序代码中,当您使用print而不是return它工作正常,因为它不会破坏循环或函数。 But when Python encounters a return, it returns the value that we've asked it to return and concludes that the function end has been reached.但是当 Python 遇到 return 时,它会返回我们要求它返回的值,并得出结论:已到达函数结尾。 It doesn't continue the loop and return a value again and again.它不会继续循环并一次又一次地返回一个值。

In order to correct your code, use any data structure in python that could hold multiple values such as a tuple or a list and append any value you need to return to that within the loop and once the loop is complete, return the data structure.为了更正您的代码,请在 python 中使用任何可以保存多个值(例如元组或列表)的数据结构,并附加您需要在循环中返回的任何值,一旦循环完成,返回数据结构。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM