Line continuation and comments

avi.e.gross at gmail.com avi.e.gross at gmail.com
Thu Feb 23 16:12:06 EST 2023


Good example, Rob, of how some people make what I consider RELIGIOUS edicts that one can easily violate if one wishes and it makes lots of sense in your example.

Let me extend that. The goal was to store a character string consisting of multiple lines when printed that are all left-aligned. Had you written:

     HelpText = """
Left click:             Open spam
...
Shift + Right click:    Fry egg
"""
Then it would begin with an extra carriage return you did not want. Your example also ends with a carriage return because you closed the quotes on another line, so a \ on the last line of text (or moving the quotes to the end of the line) would be a way of avoiding that.

Consider some alternatives I have seen that are in a sense ugly and may involve extra work for the interpreter unless it is byte compiled once.

def someFunc():
     HelpText =
     "Left click:             Open spam" + "\n" +
     "Shift + Left click:     Cook spam" + "\n" +
     ...

Or the variant of:
HelpText =  "Left click:             Open spam\n"
HelpText +=  " Shift + Left click:     Cook spam\n"
...

Or perhaps just dumping the multi-line text into a file beforehand and reading that into a string!

def someFunc():

The backslash is not looking like such a bad idea! LOL!

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Rob Cliffe via Python-list
Sent: Wednesday, February 22, 2023 2:08 PM
To: python-list at python.org
Subject: Re: Line continuation and comments



On 22/02/2023 15:23, Paul Bryan wrote:
> Adding to this, there should be no reason now in recent versions of 
> Python to ever use line continuation. Black goes so far as to state 
> "backslashes are bad and should never be used":
>
> https://black.readthedocs.io/en/stable/the_black_code_style/future_sty
> le.html#using-backslashes-for-with-statements

def someFunc():
     HelpText = """\
Left click:             Open spam
Shift + Left click:     Cook spam
Right click:            Crack egg
Shift + Right click:    Fry egg
"""

The initial backslash aligns the first line with the others (in a fixed font of course).
Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list