Since I have a pathological hatred of while True loops, I suggest this simplification of @kindall's answer: first, change some_function() so that it returns False on failure, instead of throwing an exception. Then use the following loop:
for cl in range(0, 10): while not some_function(cl): pass
If you cannot (or do not want) change some_function() , you can add a wrapper:
def exceptionless_function(arg): try: some_function(arg) return True except <known exceptions>: return False
EDIT: I added <known exceptions> above to indicate that an unqualified except clause should be avoided. If you don't know what types of exceptions you actually expect to catch, then simply calling the function again is almost certainly a misnomer in some cases. For example, the OP mentions (in a comment) that there will be a βwarningβ if a timeout occurs during the retry of this function. If this warning takes the form of some kind of exception, then it will simply be ignored in the case of "just"!
source share