Too much code - slicing

Bas wegwerp at gmail.com
Sat Sep 18 01:30:56 CEST 2010


On Sep 17, 10:01 pm, Andreas Waldenburger <use... at geekmail.INVALID>
wrote:
> On Thu, 16 Sep 2010 16:20:33 -0400 AK <andrei.... at gmail.com> wrote:
>
> > I also like this construct that works, I think, since 2.6:
>
> > code = dir[int(num):] if side == 'l' else dir[:-1*int(num)]
>
> I wonder when this construct will finally start to look good.


Using IFs is just plain ugly. Why not go for the much more pythonic

code = (lambda s:dir[slice(*(s*int(num),None)[::s])])(cmp('o',side))

Much easier on the eyes and no code duplication ... ;)

Bas



More information about the Python-list mailing list