[Python-3000] self-contained exceptions

Brett Cannon brett at python.org
Thu Jan 4 20:36:42 CET 2007


On 1/4/07, Neil Toronto <ntoronto at cs.byu.edu> wrote:
>
> Guido van Rossum wrote:
> > On 1/4/07, Phillip J. Eby <pje at telecommunity.com> wrote:
> >
> >> At 01:41 AM 1/4/2007 -0600, Ka-Ping Yee wrote:
> >>
> >>> How about this?
> >>>
> >>>      except ExcType, e:
> >>>          try:
> >>>              # body
> >>>          finally:
> >>>              e = None
> >>>
> >> It's a little bit more difficult to explain in the reference manual.  I
> was
> >> figuring we'd say that the exception variable is bound only in the body
> of
> >> the except clause; saying it becomes None when you exit sounds weirder
> to
> >> me.  How about:
> >>
> >>       except ExcType, e:
> >>           try:
> >>               # body
> >>           finally:
> >>               e = None
> >>               del e
> >>
> >> Then we get the best of all three worlds: a clean explanation, a clean
> >> implementation, and a pure source-to-source transformation.
> >>
> >
> > Great! We need (a) an update to PEP 3100 and (b) a patch to implement
> > this. (Once we have this we can talk about the rest of PEP 344;
> > perhaps that PEP should also target Py3k only?)
>
> Does it bother anybody that the variable e now has only block scope
> rather than function scope like (almost) every other local? Do the
> benefits outweigh the inconsistency?


Nope, doesn't bother me.

One could argue that there's precedent with generator comprehensions
> (also list comprehensions in Py3k), but to me this *looks* more like a
> for loop. As a first-time user I would expect it to behave the same way.


Well, how often does one reuse the exception variable past the 'execpt'
clause?  I admit I have used the leaked loop variant from a 'for' loop on
occasion, but that is not a big deal since I could get around it very
easily.

I think having the docs explicitly state the variable is local to the
'except' clause only is not that hard of a thing to grap.  We can also say
that if people really need the exception to stick around they can assign it
to another variable.

-Brett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20070104/385ebd4d/attachment-0001.htm 


More information about the Python-3000 mailing list