[英]Python: Use returned values from a function to another function
With the simple function I am able to get data stored in response variable. 通过简单的功能,我可以获取存储在响应变量中的数据。 With a print statement I can get the data showing correctly.
使用打印语句,我可以正确显示数据。
ec2_client = boto3.client("ec2")
def instance_list(instance_name):
response = ec2_client.describe_instances(
Filters=[
{
'Name': 'tag:Name',
'Values': [ instance_name ]
}
]
)['Reservations']
return response
#print(response)
if __name__ == '__main__':
my_instance_list = instance_list("example-*")
However while trying to import the value of response from the above function to another function, getting error as NameError: name 'response' is not defined
但是,当尝试将上述函数的响应值导入到另一个函数时,由于
NameError: name 'response' is not defined
出现错误NameError: name 'response' is not defined
def my_list():
list = instance_list(response)
print(list)
Looks like something unidentified. 看起来像是未知的东西。
您需要将变量传递给下一个函数,例如
my_list(instance_list())
the basic idea is to use return value of one function to another function 基本思想是使用一个函数的返回值到另一个函数
Your function should be this: 您的功能应为:
def my_list():
# List is equal to the response value of instance list.
list = instance_list("example-*")
print(list)
def add_five_to_number(number):
number += 5
return number
def print_number(number):
higher_number = add_five_to_number(number)
print(higher_number)
test_number = 3
print_number(test_number)
# Returns 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.