On Sat, May 1, 2021 at 2:52 AM Jonathan Fine <jfine2...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VQKC5FTTK4HORMA6MARDEKKCEBROOZCR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to