FAQ

On Wed, Sep 9, 2015 at 3:49 AM, Sven R. Kunze wrote:
On 08.09.2015 12:00, Wolfgang Maier wrote:


head = 99
data = myList(range(10))
s = '{}, {:*, }'.format(head, data)
# or
s2 = '{}{sep}{:*{sep}}'.format(head, data, sep=', ')
print(s)
print(s2)
# 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Thoughts?

I like it and I agree this is an oft-used pattern. From my experience I can
tell patterns are workarounds if a language cannot handle it properly.

I cannot tell what a concrete syntax would exactly look like but I would
love to see an easy-to-read solution.

It looks tempting, but there's a reason Python has join() as a
*string* method, not a method on any sort of iterable. For the same
reason, I think it'd be better to handle this as a special case inside
str.format(), rather than as a format string of the iterables; it
would be extremely surprising for code to be able to join a list, a
tuple, a ListIterator, or a generator, but not a custom class with
__iter__ and __next__ methods. (Even more surprising if it works with
some standard library types and not others.) Plus, it'd mean a lot of
code duplication across all those types, which is unnecessary.


It'd be rather cool if it could be done as a special format string,
though, which says "here's a separator, here's a format string, now
iterate over the argument and format them with that string, then join
them with that sep, and stick it in here". It might get a bit verbose,
though.


ChrisA

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 29 | next ›
Discussion Overview
grouppython-ideas @
categoriespython
postedSep 8, '15 at 10:00a
activeSep 10, '15 at 10:27a
posts29
users10
websitepython.org

People

Translate

site design / logo © 2017 Grokbase