
On Sat, May 1, 2021 at 2:52 AM Jonathan Fine <jfine2358@gmail.com> wrote:
Hi David
I see where you are coming from. I find it helps to think of sep.join as a special case. Here's a more general join, with sep.join equivalent to genjoin(sep, '', '').
def genjoin(sep, left, right): def fn(items): return left + sep.join(items) + right return fn
Here's how it works
genjoin('', '', '')('0123') == '0123' genjoin(',', '', '')('0123') == '0,1,2,3' genjoin(',', '[', ']')('0123') == '[0,1,2,3]'
All of these examples of genjoin can be thought of as string comprehensions. But they don't fit into your pattern for a string comprehension literal.
For those cases where you're merging literal parts and generated parts, it may be of value to use an f-string:
f"[{','.join('0123')}]" '[0,1,2,3]'
The part in the braces is evaluated as Python code, and the rest is simple literals. ChrisA