what is the use-case of Python3 only `if` and `break` in `for`?


I am learning python but


I am watching the code of PIL of python then I can not understand here.

            for s in [8, 4, 2, 1]:
                if scale >= s:

Is the PIL library wrong?

I did:

watch the code and think about it.

Asked By: tatsu



The variable s will remain in scope after the loop, and contain the first value of that list which is smaller than or equal to scale.

In other words, this code "rounds down" scale to one of these four specific numbers.

I might have done s = next(s for s in [8, 4, 2, 1] if scale >= s) to make that more apparent, but this loop is probably slightly more performant.

Answered By: deceze