open() and EOFError

Chris Angelico rosuav at gmail.com
Mon Jul 7 19:29:24 CEST 2014


On Tue, Jul 8, 2014 at 3:25 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Mon, Jul 7, 2014 at 2:09 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> But if the code's more complicated and it's not so easy to split, then
>> sure, doesn't seem a problem. It's like spam[foo//bar] and then
>> catching either IndexError or ZeroDivisionError - there's no big
>> confusion from having two distinct sources of two distinct errors
>> handled by two distinct except blocks.
>
> It's good practice to keep your try blocks as narrow as possible in
> any case.  One wouldn't normally expect them, but a custom __getitem__
> *could* cause a ZeroDivisionError, and a custom __floordiv__ *could*
> cause an IndexError.

Yeah, in theory. But it's not normal, and if you always break
everything out to keep exception-catching scope as narrow as possible,
you begin to lose readability in other ways. So I wouldn't object to
seeing those two combined.

ChrisA



More information about the Python-list mailing list