More elegant way to try running a function X times?

Boris Borcic bborcic at gmail.com
Thu Nov 20 12:45:33 EST 2008


Tim Chase wrote:

>> success = None
>> for i in range(5):
>>     #Try to fetch public IP
>>     success = CheckIP()
>>     if success:
>>         break
>> if not success:
>>     print "Exiting."
>>     sys.exit()
> 
> Though a bit of an abuse, you can use
> 
>   if not any(CheckIP() for _ in range(5)):
>     print "Exiting"
>     sys.exit()

I don't see why you speak of abuse, bit of abuse would be, say if you replaced 
range(5) by '12345' to win a char; but otoh I think you misspelled any() for all().

Cheers BB




More information about the Python-list mailing list