dimanche 4 novembre 2018

It it more Pythonic to use "continue" than "elif" in loops? [on hold]

Suppose I have a loop, and set of conditions to respond to:

for foo in bar:

    if first_condition(foo):
        do_first_thing()

    elif second_condition(foo):
        do_second_thing()

    else:
        do_last_thing()

I could accomplish the same thing using continue:

for foo in bar:

    if first_condition(foo):
        do_first_thing()
        continue

    if second_condition(foo):
        do_second_thing()
        continue

    do_last_thing()

Is either solution here more Pythonic? Intuitively I would use the first solution, but the use of elif makes it seem possibly less idiomatic.

Aucun commentaire:

Enregistrer un commentaire