Evaluate once or every time

Thomas Passin list1 at tompassin.net
Fri Feb 24 18:42:39 EST 2023


On 2/24/2023 5:35 PM, avi.e.gross at gmail.com wrote:
> 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']

Why be amazed? The .split() version is way easier to type.  I do it all 
the time.  Usually these evaluations will be done only once, at compile 
time, so the cost in computing time is usually not important.  And, if 
you get right down to it, the first form is actually what you mean.  The 
second form is an implementation detail (you need to feed a list of 
words into a function).

> 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 don't want to have to type these monsters, and in this form I'd be 
prone to making an error.  And they are hard to proofread.  Anyway, a 
list like this is typically used to assess whether a character is in one 
or the other group, and you can do it without turning the string into a 
list of characters:

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]

Again, much easier to type and to read, closer to what is really meant, 
and less error-prone.

In the case of the consonants, even in string form it would be hard to 
proofread, so I'd be inclined to insert spaces for readability:

CONSONANTS = 'BCDFGH JKLMNP QRSTVWX YZ bcdfgh jklmnp qrstvwx yz'

If for some reason spaces wouldn't work, I'd delete them:
CONSONANTS  = CONSONANTS.replace(' ', '')

In concept this isn't much different from using the C pre-processor to 
pre-compute some things for you.

> 
> 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. 😉



More information about the Python-list mailing list