[Python] Is an if/else better or worse than just an if at the end of a function? [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    We had an argument at work today about how to end function returns for a specific case. To illustrate: Here are two examples

    Example 1

    def my_function(happy):
    if(happy):
    return ":)"
    else:
    return ":("


    Example 2

    def my_function(happy):
    if(happy):
    return ":)"
    return ":("


    Which one is better? We came to the conclusion that the first example is more readable, but the second one has one less line. So it essentially comes down to performance, however negligible that performance increase might be. And which one is used most or is standard practice and why? Or is it just personal preference?

    Continue reading...

Compartilhe esta Página