On 2018-04-21 19:02, Tim Peters wrote:
[Matthew Woodcraft
] I would like to suggest one more motivating example for "Capturing condition values": multiple regex matches with 'elif'.
if match := re.search(pat1, text): print("Found one:", match.group(0)) elif match := re.search(pat2, text): print("Found two:", match.group(0)) elif match := re.search(pat3, text): print("Found three:", match.group(0))
Without assignment expressions, you have an annoying choice between a cascade of 'else's with an ever-increasing indent and evaluating all the matches up front (so doing unnecessary work).
That's a reasonable use, but would more likely be written like so today:
for tag, pat in (("one", pat1), ("two", pat2), ("three", pat3). ("four", pat4), ...): match = re.search(pat, text) if match: print("Found", tag + ":", match.group(0)) break
Well, that's a reason to make the example a bit more realistic, then. Say: if match := re.search(pat1, text): do_something_with(match.group(0)) elif match := re.search(pat2, text): do_something_else_with(match.group(0), match.group(1)) elif match := re.search(pat3, text): do_some_other_things_with(match.group(0)) and_also_with(match.group(1), match.group(2)) -M-