retry in exception

MonkeeSage MonkeeSage at gmail.com
Fri Sep 29 14:37:40 EDT 2006


Sybren Stuvel wrote:
> Antoine De Groote enlightened us with:
> > I hope I don't upset anybody by comparing Python to Ruby (again). Is
> > there something like Ruby's retry keyword in Python?
>
> Please don't assume that everybody knows Ruby through and through...

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

I don't think python has any equivalent (could be wrong). You can do
something like this however:

b=0
def do_stuff():
  print 1/b
try:
  do_stuff()
except:
  b=1
  do_stuff()

Regards,
Jordan




More information about the Python-list mailing list