import sys import time class Baz: def hello(self): print(foo) if True: foo = 'bar' print("hello, {0}".format('world!')) c = Baz() c.hello() success = False retry = 3 while not success and retry > 0: try: #raise Exception('my') success = True except Exception as e: print(e, file=sys.stderr) time.sleep(1) retry = retry - 1