|
|
|
|
|
by jreese
1473 days ago
|
|
I actually just used it to what I would say was a perfect example of where it legitimately improved code readability while maintaining pythonic constructs: values = [
value
for line in buffer.readlines()
if (value := line.strip())
]
Previously, I would have needed to either duplicate effort like: values = [
line.strip()
for line in buffer.readlines()
if line.strip()
]
Or used a sub-generator: values = [
value
for value in (
line.strip() for line buffer.readlines()
)
if value
]
Or rewritten it altogether using a (slower) for loop calling append each time: values = []
for line in buffer.readlines():
line = line.strip()
if line:
values.append(line)
The assignment expression is perfect for this sort of use case, and is a clear win over the alternatives IMO.Edit: fixed initial example |
|