简体   繁体   中英

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

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)

Easy example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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