3 ms·
The first example would actually be equivalent to something like while True: bytes = io.get(x) if not bytes: break ...
by Dunnorandom 8y ago
The first example would actually be equivalent to something like
while True:
bytes = io.get(x)
if not bytes:
break
...
which I think is objectively less readable.
In the second example, you have an extra call to foo for every element of stuff. If foo(z) is expensive, you'd probably want to write this as
[bar(x) for x in map(foo, stuff) if x]
instead - which I personally don't mind, but it's arguably not as clear as having the in-line assignment expression.
- amorousf00p 8y agoQuibbles and bits. Python is the only language where I write logic and then massage data structures and outputs + design 'cooler' ways to create these for an extra hour -- a week after it is in production.
- gshulegaard 8y agoI wasn't really considering the repeated genearation on `io` in the `while` example...so now I see a clear benefit to the syntax. I guess I would say I am now lukewarm about adding this additional operator. It makes at least one logical structure nicer...but I wouldn't have been heartbroken if it wasn't accepted though.
- roman_g 8y agoAnd if foo(z) is consuming a generator somehow (e.g. it's next(g)) then you can't call it twice without side effects.