[Python-ideas] New explicit methods to trim strings

Stephan Hoyer shoyer at gmail.com
Tue Apr 2 22:06:25 EDT 2019


On Tue, Apr 2, 2019 at 5:43 PM Eric V. Smith <eric at trueblade.com> 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}")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190402/4e7f0f5b/attachment-0001.html>


More information about the Python-ideas mailing list