[Python-ideas] Retrying EAFP without DRY

Jim Jewett jimjjewett at gmail.com
Wed Jan 25 16:32:35 CET 2012


On Mon, Jan 23, 2012 at 10:18 PM, Stephen J. Turnbull
<stephen at xemacs.org> wrote:
> Jim Jewett writes:

>  > So it doesn't seem like a loop because you hope to do it only once?

> With s/hope/expect/, that hits the nail on the head.

> I don't think syntax can express that cleanly, but I can't help
> thinking it's of good thing if somebody like Mike tries to find a way.
> He might succeed!

Ah...

I have often wanted a clean way to indicate "This branch can happen,
but isn't normal."  (Well, besides a comment that might be seen as
condescending if it is *obviously* an edge case.)  The retry proposal
is just specializing that for when the weird branch includes a loop.

Right now, the best I can do is hope that the special case (and
possibly the normal case, if it is repeated) can be factored out, so
that I can write

    if not doit(args):
        if not doit(tweak1(args)):
            if not doit(tweak2(args)):
                raise ReallyCant(args)

or

    if oddcase(args):
        handle_oddcase(args)
    else:
        # Alternatively, make this suite much longer, so that it is
        # "obviously" the main point of the function.
        return _real_function(args)


That said, I've wanted unusual-case annotation more when I thought the
compiler might use the information.  Without compiler support, I'm not
sure how much takeup there would be for the resulting
documentation-only construct.

-jJ



More information about the Python-ideas mailing list