Return in python 3

Asked

Viewed 307 times

1

I am creating a function in python3 and a question arose about the use of Return, when we call him in the code he must terminate the function immediately?

In the code below this is not working, even when the if is satisfied, it finishes the entire loop of the code, to then give the return.

def find_msg_by_parameter(parameter, search_term, email_folder):
    client = client_connect()
    results = client.users().messages().list(userId='me', labelIds = [email_folder]).execute()
    for id_mail in results['messages']:
        id_msg = client.users().messages().get(userId='me', id=id_mail['id']).execute()
        id_msg_df = pd.DataFrame(id_msg['payload']['headers'])
        msg_header = id_msg_df[id_msg_df.name==parameter].value.iloc[0]
        if check_name(search_term,msg_header):
            return id_mail
        print('Buscando')
    print('Não encontrado')

The function check_name returns a boolean.

1 answer

2

The Return closes the execution of the current function.

Do some tests to see the behavior of Return.. For example:

def test_return(var1, var2):
    print("Entrou")
    if var1 < var2:
        return "Menor"
    else:
        print("Maior")
    print("Depois do if")
    return "Saiu"

print(test_return(1, 2))
print("\n Chama a funcao novamente: \n")
print(test_return(3, 2))
  • 1

    I was able to find out what the problem was, the list that is iterated is generated with random ordering, the Return was working correctly, I did not pay attention to this detail. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.