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 :
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
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']mylist = "The quick brown fox jumps over the lazy dog".split()
mylist
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.way of avoiding that.
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
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@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
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()
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']mylist
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. 😉
VOWELS = 'aeiouAEIOU'
is_vowel = 'y' in VOWELS
If I really needed them to be in a list, I'd probably do a list comprehension:
VOWEL_LIST = [ch for ch in VOWELS]
On 2023-02-24 at 18:42:39 -0500,
Thomas Passin <list1@tompassin.net> wrote:
VOWELS = 'aeiouAEIOU'
is_vowel = 'y' in VOWELS
If I really needed them to be in a list, I'd probably do a list
comprehension:
VOWEL_LIST = [ch for ch in VOWELS]
Why use a comprehension when a simple loop will do? ;-)
No. Wait. That's not what I meant.
Why use a comprehension when the constructor will do?
VOWEL_LIST = list(VOWELS)
VOWELS = 'aeiouAEIOU'
is_vowel = 'y' in VOWELS
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 40:35:37 |
Calls: | 10,392 |
Files: | 14,064 |
Messages: | 6,417,205 |