Line continuation and comments

Mark Bourne nntp.mbourne at spamgourmet.com
Fri Feb 24 16:04:12 EST 2023


Personally, I don't particularly like the way you have to put multiline 
strings on the far left (rather than aligned with the rest of the scope) 
to avoid getting spaces at the beginning of each line.  I find it makes 
it more difficult to see where the scope of the class/method/etc. 
actually ends, especially if there are multiple such strings.  It's not 
too bad for strings defined at the module level (outer scope) though, 
and of course for docstrings the extra spaces at the beginning of each 
line don't matter.

However, rather than using "+" to join strings as in your examples 
(which, as you suggest, is probably less efficient), I tend to use 
string literal concatenation which I gather is more efficient (treated 
as a single string at compile-time rather than joining separate strings 
at run-time).  See 
<https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation>.

For example:
       HelpText = ("Left click:             Open spam\n"
                   "Shift + Left click:     Cook spam\n"
                   "Right click:            Crack egg\n"
                   "Shift + Right click:    Fry egg\n")

The downside is having to put an explicit "\n" at the end of each line, 
but to me that's not as bad as having to align the content to the far left.

Getting a bit more on topic, use of backslashes in strings is a bit 
different to backslashes for line continuation anyway.  You could almost 
think of "\
(newline)" in a multiline string as being like an escape sequence 
meaning "don't actually put a newline character in the string here", in 
a similar way to "\n" meaning "put a newline character here" and "\t" 
meaning "put a tab character here".

Mark.


avi.e.gross at gmail.com wrote:
> 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