3 ms·
>I'll add that := fixes something I truly hate: the lack of `do until` in Python, which strikes me as deeply un-Pythonic. Am I supposed to break out of `while T
by owlowlowls 7y ago
>I'll add that := fixes something I truly hate: the lack of `do until` in Python, which strikes me as deeply un-Pythonic. Am I supposed to break out of `while True`? Am I supposed to set the variable before and at the tail of the loop (a great way to add subtle typos that won't cause errors)?
This is relevant to what I've been doing in OpenCV with reading frames from videos! In tutorial examples on the web, you'll see exactly the sort of pattern that's outlined in the PEP 572 article.
>line = f.readline()
>while line:
> ... # process line
> line = f.readline()
Just, replace readline() with readframe() and the like. So many off-by-one errors figuring out when exactly to break.
- kbd 7y agoFor sure. Walrus operator is "unnecessary" but is a clear improvement in the code that will use it.
- Aramgutang 7y agoThat example can also be tackled with Python's little-known feature of calling the built-in `iter` with a second argument: > for line in iter(f.readline, ''): > ... # process line See: https://docs.python.org/3/library/functions.html#iter https://docs.python.org/3/library/functions.html#iter
- jl2718 7y agoThis is useful, but two totally different functions with the same name distinguished only by the presence, not even the value of an unused argument. Where else does anything like this exist in the language? Seems problematic.
- dragonwriter 7y ago> This is useful, but two totally different functions with the same name distinguished only by the presence, not even the value of an unused argument. The sentinel argument is not unused. > Where else does anything like this exist in the language? In the narrow sense of “in the language” (builtins only), it's unique. There may be other examples in stdlib. That aside, functions that do a descriptively similar thing (i.e.,“build an iterator”) but where the details of how they do it differ significantly by aritt aren't weird among programming languages generally; don't see why it would be particularly problematic.