[Python-ideas] Inline assignments using "given" clauses

Tim Peters tim.peters at gmail.com
Mon May 14 23:32:20 EDT 2018


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;-)

It doesn't work as well with assignment expressions even if they were
(re)generalized to allow unpacking syntax.  In this specific case, I'd
keep the original loop-and-a-half rather than do:

    while (t := s.partition(sep})[1]:
        head, matched, s = t

With unpacking syntax restored, same answer:

    while (head, matched, s := s.partition(sep})[1]:

or
    while [(head, matched, s := s.partition(sep}), matched][-1]:

to combine the worst annoyances of everything and add another ;-)


More information about the Python-ideas mailing list