Re: computing with characters



Torsten Bronger <bronger@xxxxxxxxxxxxxxxxxxxxx> wrote:

The biggest ugliness though is ",".join(). No idea why this should
be better than join(list, separator=" "). Besides, ",".join(u"x")
yields an unicode object. This is confusing (but will probably go
away with Python 3).

It is only ugly because you aren't used to seeing method calls on string
literals. Here are some arguably less-ugly alternatives:

print str.join(", ", sequence)

or:

comma_separated = ", ".join

will let you use:

print comma_separated(sequence)

or even just:

SEPARATOR = ", "

followed by:

SEPARATOR.join(sequence)

is no more ugly than any other method call.

It would make perfect sense for join to be a method on stringlike objects
if it simply returned an object of the same type as the object it is called
on. As you point out, where it breaks down is a str separator can return a
unicode result and that is confusing: if you want a unicode result perhaps
you should be required to use a unicode separator but that isn't going to
happen (at least not in Python 2.x).

What definitely wouldn't make sense would be to make join a method of the
list type (as it is in some other languages).
.



Relevant Pages