[Python-ideas] Inline assignments using "given" clauses
Tim Peters
tim.peters at gmail.com
Tue May 15 13:57:07 EDT 2018
[Tim]
>> Just noting some real code I typed today where `given` works great if
>> it allows unpacking syntax, and assignment expressions don't:
>>
>> while True:
>> head, matched, s = s.partition(sep)
>> if not matched:
>> break
>>
>> Using `given`:
>>
>> while matched given head, matched, s = s.partition(sep):
>>
>> Typing "matched " twice still sucks, though;-)
>>
> [snip]
[MRAB <python at mrabarnett.plus.com>]
> If you're using .partition multiple times, you might as well use .split
> instead!
Possibly - depends on what the rest of the loop is doing. In this
specific case, there are paths in the loop body that change what `sep`
is bound to, so I'd have to pass `maxsplit=1` to get a clumsier way to
do what `partition()` does directly, For example,
>>> "a+b".split("+", maxsplit=1) # separator wholly contained
['a', 'b']
>>> "a-b".split("+", maxsplit=1) # separator doesn't appear at all
['a-b']
I'ts more convenient that `partition()` always returns a 3-tuple, not
sometimes a 1-list and other times a 2-list.
More information about the Python-ideas
mailing list