On Tue, Apr 2, 2019 at 5:43 PM Eric V. Smith <[email protected]> wrote:

> PS: I really tried to find a way to use := in this example so I could
> put the assignment inside the 'if' statement, but as I think Tim Peters
> pointed out, without C's comma operator, you can't.
>

Conceivably cut_prefix could return None if not found. Then you could write
something like:

if (stripped := cut_prefix(line, "INFO>")) is not None:
     print(f"control line {stripped!r}")
else:
     print(f"data line {line!r}")

You could even drop "is not None" in many circumstances, if you know the
cut string will be non-empty. That's actually pretty readable:

if stripped := cut_prefix(line, "INFO>"):
     print(f"control line {stripped!r}")
else:
     print(f"data line {line!r}")
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to