Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am learning python but

https://github.com/python-pillow/Pillow/blob/main/src/PIL/JpegImagePlugin.py#L435-L437
https://github.com/python-pillow/Pillow/blob/31669013d420c2a532f5be6c5631827fe36e6f70/src/PIL/JpegImagePlugin.py#L435-L437

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Is the PIL library wrong?

I did:

watch the code and think about it.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading