简体   繁体   中英

How to use a returned list from one function in another?

If I have a function and it returns three lists, how can I reference or call one of the lists returned?

For example, I am using this API that and I am returning three lists, ids, names, emails . Is there a way to just reference ids? I tried users_info(ids) , but that didn't work. It just gives a NameError that the name 'ids' is not defined.

def users_info():

    url = 'https://slack.com/api/users.info'
    headers = {'Accept': 'application/x-www-form-urlencoded'}

    users = channels_info() # the user IDs from channels.info
    ids = []
    names = []
    emails = []
    # loops for every user
    for user in users:
        payload = {
            'token': API_KEY,
            'user': user
        }
        r = requests.get(url, headers=headers, params=payload)
        id = r.json()['user']['id']
        email = r.json()['user']['profile'].get('email') 
        name = r.json()['user']['real_name']

        if r.status_code == 200:
            # This is to ignore the users that don't have an email field
            if email is not None:
                ids.append(id)
                emails.append(email)
                names.append(name)
        else:
            return None
    # result returns a list of ids, names, and emails of the users
    return ids, names, emails

I am trying to use the ids returned from users_info() as part of the payload of another API, but I also still need the names and emails, so those are also returned along with the ids.

Here is one way:

ids, names, emails = user_info()
# Use ids here

Do this,

ids, names, emails = user_info()     

Here's a demonstration (an example) regarding multiple returns:

 def func():
 ...     return ['a','b'], ['c','d'], ['e','f']
 ...
 >>> x,y,z = func()
 >>> x
 ['a', 'b']
 >>> y
 ['c', 'd']
 >>> z
 ['e', 'f']

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