Yield comprehension

I think it’s both the structure and the layout. And the or hack.

I remember reading it and having to change my understanding when I arrived at your if another_check()\, which appears after the two things it leads to (the yielding loop and the hacked-on get_value()) and is in my opinion too far indented (I’d rather expect the if at the same indentation as the previous else, not at the same indentation as the or, which makes it looks like it’s even inside the parentheses along with the yielding loop and the or-added expression).

This appended if two lines down and that far indented made me realize only at that point that the previous else is really rather an elif. With the if part “hidden” later/deeper. This reminded me of “Sentences should be readable from left to right without ambiguity” from Knuth et al.'s “Mathematical Writing” - I don’t like when I have to rethink the first half of a long sentence/expression because of something in the middle, I prefer to read and understand it “from left to right”.

So with something this long, I’d prefer the if/elifs/else structure to be clearly visible at first sight. I’d write it like this:

if some_check():
    get_some_value()
elif another_check():
    for object_ in iterable:
        yield <expression>
    get_value()
elif ...:
    get_other_value()
else ...:
    ...
4 Likes