For review: PEP 308 - If-then-else expression

Andrew Dalke adalke at mindspring.com
Mon Feb 10 00:40:22 EST 2003


Paul Rubin:
> I'd say every time I write code with regexps, I want an assignment
> expression like
>
>    while (m := re.match(exp, something)): ...

You can do that with the variable leakage in list comprehensions

s = "ab abb abbb abb"
pat = re.compile(r"(ab*)")

while [m for m in [pat.search(s)] if m]:
  print m.group(0)
  s = s[m.start(0) + 1:]

which yields

ab
abb
abbb
abb

But yes, this is a hack just like the ones used to emulate the
ternary if/else expression.  ;)

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list