Clever hack or code abomination?

Vito 'ZeD' De Tullio zak.mc.kraken at libero.it
Thu Dec 1 15:35:02 EST 2011


Arnaud Delobelle wrote:

> 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 ""
>     ....
> 

obfuscated for obfuscated, you can merge the two ideas:

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


-- 
By ZeD




More information about the Python-list mailing list