How clean/elegant is Python's syntax?
Alister
alister.ware at ntlworld.com
Fri May 31 05:08:43 EDT 2013
On Thu, 30 May 2013 20:38:40 +0100, MRAB wrote:
> On 30/05/2013 19:44, Chris Angelico wrote:
>> On Fri, May 31, 2013 at 4:36 AM, Ian Kelly <ian.g.kelly at gmail.com>
>> wrote:
>>> 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)
>>
>> Trouble is, it makes some sense either way. I often put the larger
>> argument first - for instance, I would write 123412341324*5 rather than
>> the other way around - and in this instance, it hardly seems as
>> clear-cut as you imply. But the function can't be written to take them
>> in either order, because strings are iterable too. (And functions that
>> take args either way around aren't better than those that make a
>> decision.)
>>
> And additional argument (pun not intended) for putting sep second is
> that you can give it a default value:
>
> def join(iterable, sep=""): return sep.join(iterable)
I think that is the winning argument.
Next question is what should be the default ("", " " or',')?
--
Nasrudin walked into a teahouse and declaimed, "The moon is more useful
than the sun."
"Why?", he was asked.
"Because at night we need the light more."
More information about the Python-list
mailing list