kyub@discuss.tchncs.de to Programmer Humor@programming.devEnglish · edit-21 day agoThe infamous "if loop" actually existsdiscuss.tchncs.deimagemessage-square24fedilinkarrow-up1158arrow-down19file-text
arrow-up1149arrow-down1imageThe infamous "if loop" actually existsdiscuss.tchncs.dekyub@discuss.tchncs.de to Programmer Humor@programming.devEnglish · edit-21 day agomessage-square24fedilinkfile-text
minus-squarewise_pancakelinkfedilinkarrow-up7·6 hours agoDid you know python for loops have an else clause? Did you also know it runs after the loop iterator is done, and not when there was never anything to iterate over at all? https://docs.python.org/3/reference/compound_stmts.html#for
minus-squareVigge93@lemmy.worldlinkfedilinkarrow-up1·2 hours agoYour point about it not running when there is nothing to iterate over is incorrect. The else-statement runs when the iterator is exhausted; if the iterator empty, it is exhausted immediately and the else-statement is executed.
minus-squarelugal@sopuli.xyzlinkfedilinkarrow-up2·42 minutes agoI think it’s intended as “not only when” because it would make sense to have an “if empty” case but the way it is, it doesn’t make sense
minus-squareqaz@lemmy.worldlinkfedilinkEnglisharrow-up5·5 hours agoThat actually seemed quite useful at first.
Did you know python for loops have an else clause?
Did you also know it runs after the loop iterator is done, and not when there was never anything to iterate over at all?
https://docs.python.org/3/reference/compound_stmts.html#for
Your point about it not running when there is nothing to iterate over is incorrect. The else-statement runs when the iterator is exhausted; if the iterator empty, it is exhausted immediately and the else-statement is executed.
I think it’s intended as “not only when” because it would make sense to have an “if empty” case but the way it is, it doesn’t make sense
That actually seemed quite useful at first.