[Python-ideas] Retrying EAFP without DRY (original) (raw)
Steven D'Aprano steve at pearwood.info
Sat Jan 21 09:57:16 CET 2012
- Previous message: [Python-ideas] Retrying EAFP without DRY
- Next message: [Python-ideas] Retrying EAFP without DRY
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Chris Rebert wrote:
On Fri, Jan 20, 2012 at 11:47 PM, Steven D'Aprano <steve at pearwood.info> wrote:
I just wish that break and continue could be written outside of a loop, so you can factor out common code: [...] Easily accomplished: def dothething(x): try: dosomething(x) except SpamError: fixup(x) return False else: return True def tryrepeatedly(n, func, arg): for in range(n): if func(arg): break else: raise HamError('tried %d times, giving up now" % n) tryrepeatedly(5, dothething, y)
Not so easily accomplished if you need the return result from do_the_thing. Naturally you can always return a tuple
(result_I_actually_want, code_to_break_or_continue)
but that's not exactly elegant.
-- Steven
- Previous message: [Python-ideas] Retrying EAFP without DRY
- Next message: [Python-ideas] Retrying EAFP without DRY
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]