4 ms·
`do while` loops are (effectively) coming to Python. Yay!
by mrastro 8y ago
`do while` loops are (effectively) coming to Python. Yay!
- 2T1Qka0rEiPr 8y agoI was going to say - its application in this while loop is really nice I think: https://github.com/python/cpython/pull/8122/files#diff-0ad86c44e7866421ecaa5ad2c0edb0e2L946 https://github.com/python/cpython/pull/8122/files#diff-0ad86...
- mehrdadn 8y agoUnfortunately it's also directly at odds with Python's own EAFP [1] philosophy, which is to raise exceptions rather than return special values (StopIteration etc.). It's no longer clear to me which one the proper Python coding style is. Are we supposed to switch to returning special values instead of raising exceptions now? [1] https://docs.quantifiedcode.com/python-anti-patterns/readability/asking_for_permission_instead_of_forgiveness_when_working_with_files.html https://docs.quantifiedcode.com/python-anti-patterns/readabi...
- StavrosK 8y agoWhere do you see it returning special values? While loops have always stopped on a falsy value.
- mehrdadn 8y agoThe issue is the readBlah() methods. What should they do when they can't read anything? They can return either 0 or an empty result (non-special falsy value), return -1 (special truthy value), return None (special falsy value), or raise an exception. In this case it seems they chose the first option, but this is at odds with something like next(iterator), which similar but raises an exception. It's not clear to me which is the proper pattern anymore, now that they're encouraging assignment.
- duckerude 8y agoThese seem to be relatively thin wrappers around existing APIs. A more typical way to read from a file-like object is argumentless .read() to get all content, or iterating over it to get it line by line, but if you're passing an argument to .read you're doing something fairly low-level anyway. EAFP is already violated in a few cases where it makes more sense. This doesn't add new violations, it adds a cleaner way to work with them.
- nerdponx 8y agoI can't wait for EAFP to die.