|
|
Subscribe / Log in / New account

PEP 572 and decision-making in Python

PEP 572 and decision-making in Python

Posted Jul 13, 2018 22:14 UTC (Fri) by gdiscry (subscriber, #91125)
In reply to: PEP 572 and decision-making in Python by gdamjan
Parent article: PEP 572 and decision-making in Python

That's the issue with toy examples like mine and the ones in the article. If it's a common pattern there's a pretty good chance that you can do it in a simple, optimized and readable way. And then the reader misses the point because the syntax specific to the common case doesn't apply to more complex code.

A better example for iter would be to read a binary file by chunk using for chunk in iter(lambda: f.read(CHUNK_SIZE), b''):. To my knowledge, there isn't a ready made function or method that does the same, contrary to file.readline and file.__iter__.

We can also see that the code is slightly less readable than the readline example. Furthermore, the complexity increases even more once the stop condition cannot be expressed as a simple equality to a sentinel value.

I have to admit that I find while chunk := f.read(CHUNK_SIZE): more readable than the other ways to do it and it looks like the readability remains good even in more complex cases.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds