Clever hack or code abomination?

Arnaud Delobelle arnodel at gmail.com
Thu Dec 1 03:35:15 EST 2011


On 1 December 2011 03:15, Roy Smith <roy at panix.com> wrote:
> I need to try a bunch of names in sequence until I find one that works
> (definition of "works" is unimportant).  The algorithm is:
>
> 1) Given a base name, "foo", first see if just plain "foo" works.
>
> 2) If not, try "foo-1", "foo-2", and so on
>
> 3) If you reach "foo-20", give up.
>
> What would you say if you saw this:
>
> for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]:
>

It's a little obfuscated ;) I would go for the simple:

for i in xrange(21):
    suffix = "-%s" % i if i else ""
    ....

-- 
Arnaud



More information about the Python-list mailing list