[Python-ideas] Retrying EAFP without DRY

Chris Rebert pyideas at rebertia.com
Sun Jan 22 22:52:52 CET 2012


On Sun, Jan 22, 2012 at 1:30 PM, Mike Meyer <mwm at mired.org> wrote:
> On Sat, 21 Jan 2012 11:46:19 +0000
> Paul Moore <p.f.moore at gmail.com> wrote:
>> On 21 January 2012 07:47, Steven D'Aprano <steve at pearwood.info> wrote:
<snip>
>> I'd be -1 on a retry keyword that worked any way other than this
>> (simply because this is "clearly" the most obvious interpretation).
>> Whether a retry statement is worth having at all, though, is something
>> I'm not sure of - I'd like to see some real-world use cases first.
>> I've never encountered the need for it myself. And I can't honestly
>> imagine why the while True...try...break" idiom would ever not be
>> sufficient.
>
> I saw a number of request for "real world uses cases". I thought I
> covered that in the OP. This ideas was *prompted* by a real world use
> case where we wanted to wrap an exception in our own private exception
> before handling it. Because the code that would raise the exception
> - other exceptions missing the attributes we added to ours - was the
> code we want to run after handling them, we're left with LBYL or DRY
> or wrapping a loop around the code when it wasn't really a loop.

The thing is, until this message, you never described any concrete
details about your specific use-case, other than that you had one.
Now you have, but in prose (which is regrettably imprecise) rather
than code; I'm still not entirely sure what your use case looks like.
As best I can parse it, a nested try-except sounds like it would work.

People like use-cases because they can reveal motivations or
subtleties that often turn out to be significant, and they can more
easily critique them than completely abstract/general cases.

Cheers,
Chris



More information about the Python-ideas mailing list