How clean/elegant is Python's syntax?

Ian Kelly ian.g.kelly at gmail.com
Thu May 30 14:36:54 EDT 2013


On Wed, May 29, 2013 at 8:49 PM, rusi <rustompmody at gmail.com> wrote:
> On May 30, 6:14 am, Ma Xiaojun <damage3... at gmail.com> wrote:
>> What interest me is a one liner:
>> print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in
>> range(1,10)]) for j in range(1,10)])
>
> Ha,Ha! The join method is one of the (for me) ugly features of python.
> You can sweep it under the carpet with a one-line join function and
> then write clean and pretty code:
>
> #joinwith
> def joinw(l,sep): return sep.join(l)

I don't object to changing the join method (one of the more
shoe-horned string methods) back into a function, but to my eyes
you've got the arguments backward.  It should be:

def join(sep, iterable): return sep.join(iterable)

Putting the separator first feels more natural to me because I expect
the separator to usually be short as compared to the iterable, which
is often a longer expression (as is the case in both of your
subsequent usages).  Placing the separator first also preserves
consistency of interface with the str.join and bytes.join functions,
as well as the older string.join function.



More information about the Python-list mailing list