[Python-Dev] Tangent on class level scoping rules (was Re: PEP 463: Exception-catching expressions)

Chris Angelico rosuav at gmail.com
Sat Feb 22 12:17:37 CET 2014


On Sat, Feb 22, 2014 at 10:01 PM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>>
>> On Sat, Feb 22, 2014 at 10:57 AM, Greg Ewing
>> <greg.ewing at canterbury.ac.nz> wrote:
>>
>>> I'm still not convinced it would be all *that* difficult.
>>> Seems to me it would be semantically equivalent to
>>> renaming the inner variable and adding a finally clause
>>> to unbind it. Is there something I'm missing?
>>
>>
>> An inner scope should shadow rather than unbinding.
>
>
> It would. The name being unbound would be the renamed
> inner one, not the one being shadowed.

If the whole inner scope is disappearing, then there's no need to
unbind in a finally clause. The current behaviour of try/except is
exactly what you're describing, with no subscope:

Python 3.4.0rc1+ (default:9f76adbac8b7, Feb 15 2014, 20:19:30)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> e = 2.71828
>>> try: 1/0
... except ZeroDivisionError as e: pass
...
>>> e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'e' is not defined

It's been unbound from the parent scope. It's not shadowed, it's
actually overwritten.

ChrisA


More information about the Python-Dev mailing list