> In ruby, the equivalent to try...except is begin...rescue. In the > rescue section you can ask it to retry the begin section. So, for > example: > > b=0 > begin > puts 1/b > rescue > b=1 > retry # <- this little guy > end
Well, it's all a matter of how you look at it. I personally prefer to see that there's some sort of looping going on. Something crazy like this contrived example, it would be best to just check to see if b==0 beforehand. However, for the sake of example, one could do something like b = 0 for d in [b, 1]: try: print 1/d break except ZeroDivisionError: pass which would allow you to have a number of things to try...to give a little more plausibility to the idea, the original ruby might have been something like (forgive my ignorance of ruby syntax) url = 'http://www.example.com' begin get_webpage(url) rescue url = 'http://www2.example.com' retry end which would try a backup server if the main one is down. The above python variant would allow an arbitrary number of servers to be listed servers = ['http://www%i.example.com' % i for i in xrange(1,5)] for server in servers: try: u = urllib.urlopen(server) break except IOError: pass else: raise NoAvailableServersException() This comes full-circle on another thread asking about the use of "else:" after a for/while loop...yes, nice to have. The above python code will try to open each of the servers in turn until it gets one that it can open. If it can't open any of them, it raises some concocted NoAvailableServersException. To get the above to succeed, you can add servers[3] = 'http://www.example.com' which will actually exist, and thus will fall out of the loop with "u" successfully set to the connection from which you can read. I have to agree with Diez, that this sounds like some ambiguous/dangerous behaviour could ensue from such a language construct. I'd just as soon specify my desired behavior explicitly. Just a few ideas. -tkc -- http://mail.python.org/mailman/listinfo/python-list