For reasons outside the scope of this entry , I was recently reminded of some very old entries here where I compared some Python code with some Perl code to do the same thing . One of the things that stood out to me is that way back then I said:
For example, I could have written '
print "\n".join(rr.strings)' in Python, but it doesn't feel right; I would rather write theforloop explicitly instead.
At some point between back then and now, my views on this changed without me noticing. Today I would unhesitatingly print a multi-line list of text (ie a list of lines) using the .join() version, and in fact I have; I can easily find little utility programs of mine that use this idiom (some of them a significant number of years old by now, so I don't think this is a recent shift).
What I don't know is if this was a shift in my personal views or if Python in general shifted its view of this idiom. At least some Python code seems to have been using this a long time ago , so it's entirely possible that I'm what changed and this was always considered idiomatic Python.
(My suspicion today is that '
"\n".join()
' probably always was idiomatic Python, at least in Python 2 and later. It's not quite as clear as a
for
loop but it's much more compact.)
There are probably lots of other Python idioms where either I or Python as a whole has shifted our views on over time. But for various reasons I rarely get my attention shoved into them the way I did this time. We do have a certain amount of old Python code that we're still using, but because it's old and reliable, I generally don't have any reason to look at it and think about the idioms it uses.
All of this makes me wonder what Python idioms I'm currently not using and thinking about that I'll consider perfectly natural and automatic in five or ten years. I should probably be using dataclasses , and then there's copious use of typing annotations (which would probably feel more natural to me if I used them frequently).
(I have a very old and now abandoned Python program, but I'm not energetic enough to pick through its code. Also, it would probably be slightly depressing.)