can it be shorter?

Aaron Brady castironpi at gmail.com
Mon Jun 8 21:57:58 CEST 2009


On Jun 8, 9:50 am, Jean-Michel Pichavant <jeanmic... at sequans.com>
wrote:
> Aaron Brady wrote:
> > Shorter is always better.
>
> > url+= { '/': '' }.get( url[ -1 ], '/' )
>
> Why bother with spaces or 3 letter-wide token, check this  :o) :
> x+={'/':''}.get(x[-1],'/')
>
> Apart from joking, the following proposed solution is by **far** the one
> I prefer
>
> > if not url.endswith('/'):
> >    url += '/'
>
> Maybe not the shorter, but the most concise and clear to me.

Why won't Python permit:

url.endswith( '/' ) or url.append( '/' )

?  Should it?  Do we find it just as concise and clear?  Does it
outweigh the priority of the immutability of strings?  It works on
lists, for example.  A sole mutating operation could create a highly
and finely tempered compromise with immutability.  Would it be
'append'?

I like Scott's and MRAB's idea for slicing, not indexing, the last
character.

The most literal translation of the original natural language is:

>>> #ensure that the url ends with a '/'
>>> ensure( url, string.endswith, '/' )

(Is it not?)  But the parameters aren't sufficient to define 'ensure'
generally, and it won't be able to mutate 'url' regardless.



More information about the Python-list mailing list