[Python-ideas] Retrying EAFP without DRY

Paul Moore p.f.moore at gmail.com
Tue Jan 24 12:36:29 CET 2012


On 24 January 2012 11:19, Steven D'Aprano <steve at pearwood.info> wrote:
> "Whacking" is not necessarily bad or difficult, and is not necessarily a
> code smell. There are many common situations where "try again" is natural
> and expected. E.g. in response to a busy signal, you should wait a little
> while before retrying:
[...]
> Although you save one indent level, you don't save any lines of code, and it
> costs you the effort of handling the book-keeping that a for-loop would give
> you for free. I don't count this as a win.

Having read that, I agree that the "retry" doesn't buy you much. But I
do think there's a pattern in there that it would be nice to be able
to abstract out, namely the exponential back-off. The pattern of "try
X, if Y happens, then do Z and wait, then retry" is probably common
enough in certain types of application that it would be nice to
encapsulate it in a library routine. (I haven't needed it myself,
admittedly...) I can't quite see how to do it, though - I thought
about clever uses of with statements, or abstract classes with defined
callback methods you could use, but couldn't find anything obvious.
(The big issue being that "Y" is that a particular exception is raised
in this case, but may be something else in general).

A construct that let end users abstract this type of pattern would
probably be a far bigger win than a retry statement. (And it may be
that it has the benefit of already existing, I just couldn't see it
:-))

Paul.



More information about the Python-ideas mailing list