Mark,

I was very interested in the point you made and have never thought much about 
string concatenation this way but adjacency is an operator worth using.

This message has a new subject line as it is not about line continuation or 
comments.

From what you say, concatenation between visibly adjacent strings is done once 
when generating bytecode. Using a plus is supposed to be about the same but may 
indeed result in either an error if you use anything other than a string literal

bad = "hello " str(12)

or you must use something like a "+" to do the concatenation at each run time. 
Or, weirder, do it manually as : 

good = "hello ".__add__(str(12))

This may be no big deal in terms of efficiency but something to consider.

I have often stared in amazement at code like:

>>> mylist = "The quick brown fox jumps over the lazy dog".split()

>>> mylist
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

Or perhaps to make a list of vowels:

import string

vowels = list("aeiouAEIOU")
consonants = sorted(list(set(string.ascii_letters) - set(vowels)))

I mean couldn't you do this work in advance and do something like:

vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
consonants = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 
'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

I assume this latter version would be set once no matter how often you run the 
unchanged program. YES, I am aware this may be bad practice for code you want 
to adapt for international use. 

But why be wasteful? I am currently reading a book on refactoring and will not 
share if it is illustrated, or if the above is a decent example as the book 
uses examples in JavaScript. 😉



-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On 
Behalf Of Mark Bourne
Sent: Friday, February 24, 2023 4:04 PM
To: python-list@python.org
Subject: Re: Line continuation and comments

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.gr...@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....@python.org> On Behalf Of 
> Rob Cliffe via Python-list
> Sent: Wednesday, February 22, 2023 2:08 PM
> To: python-list@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_st
>> y 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
> 
--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to