[Python-Dev] Unbound locals in class scopes

Ivan Levkivskyi levkivskyi at gmail.com
Mon Jun 22 00:46:07 CEST 2015


On 21 June 2015 at 22:05, Guido van Rossum <guido at python.org> wrote:
>
> On Sun, Jun 21, 2015 at 6:22 PM, Ivan Levkivskyi <levkivskyi at gmail.com>
wrote:
>>
>> It is still not clear whether Guido's comment still stands for not
raising an UnboundLocalError in class definitions but using globals instead.
>
>
> Can you phrase this in the form of an example, showing what it currently
does and what you think it should do, instead?
>

Here is an example:

x = "xtop"
y = "ytop"
def func():
    x = "xlocal"
    y = "ylocal"
    class C:
        print(x)
        print(y)
        y = 1
func()

prints

xlocal
ytop

Intuitively, one might think that it should raise UnboundLocalError or
print ylocal instead of ytop.
This question was discussed 13 years ago and then you said that this lookup
in globals
is an intentional behavior.

This behavior is not documented, and I started an issue on bug tracker
about documenting it.
Then, Eric proposed to ask you again, whether this is still an intentional
behavior.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150622/439e660b/attachment.html>


More information about the Python-Dev mailing list