On 11/10/2020 17:20, haael wrote:

Very often I use the following construction (especially in operators):

    try:
        something
    except SomeError:
        try:
            something_else
        except AnotherError:
            try:
                something_completely_different
            except Whatever:
                return NotImplemented


I propose a construction to simplify the staircase hierarchy of exception handlers:

    try:
        something
    except SomeError try:
        something_else
    except AnotherError try:
        something_completely_different
    except Whatever:
        return NotImplemented

This would be somewhat analogous to `else if` blocks.

This pattern is very common in operator implementations. And this proposal could simplify it a bit.

haael
I sometimes encounter these somewhat analogous situations:
    (a) To accomplish a goal takes several stages; failure at any stage means the whole attempt must be abandoned.     (b) I attempt various ways to accomplish a goal; success in any way means that the others need not be attempted. which, in common with the OP's example, can lead to indefinite nesting levels. One workaround I sometimes use is a pseudo-loop which is executed once only; whenever the process can be abandoned, I `break` out of the "loop".  It ain't particularly elegant, but it works.  Applying to the OP's example we might have:

    for _ in '1': # pseudo-loop
# NB One gotcha is to write a pseudo-loop with something like "while True"
#       and then forget to put in a final `break'.
        try:
            something
            break
        except SomeError:
            pass
        try:
            something_else
            break
        except AnotherError:
            pass
        try:
            something_completely_different
        except Whatever:
           return NotImplemented

If convenient (it isn't always) the code can be hived off to a function and the `break's replaced by `return's.  If this applied to the OP's example it could maybe be written:

    try:
        something
        return <some result>
    except SomeError:
        pass
    try:
        something_else
        return <some other result>
    except AnotherError:
        pass
    try:
        something_completely_different
        return <completely different result>
    except Whatever:
        return NotImplemented

or (more verbose but cleaner if the `return` statements might raise an exception (including NameError for a misspelt variable name)):

    try:
        something
    except SomeError:
        pass
    else:
        return <some result>
    try:
        something_else
    except AnotherError:
        pass
    else:
        return <some other result>
    try:
        something_completely_different
    except Whatever:
        return NotImplemented
    else:
        return <completely different result>

I sometimes wish that Python provided nicer syntax for writing a "pseudo-loop", but I guess it's not an urgent need.  And I guess the same applies to the OP's suggestion.  So I'm -0.8 on it.
Best wishes
Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UBCVFKFFV7OYCCB4D7RUFX4VZVVVMMJ7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to