[Python-Dev] class name spaces inside an outer function

Benjamin Peterson benjamin at python.org
Sun Apr 28 05:28:36 CEST 2013


2013/4/27 Nick Coghlan <ncoghlan at gmail.com>:
> On Sun, Apr 28, 2013 at 11:38 AM, Benjamin Peterson <benjamin at python.org> wrote:
>> 2013/4/27 Nick Coghlan <ncoghlan at gmail.com>:
>>>
>>> On 28 Apr 2013 04:30, "Ethan Furman" <ethan at stoneleaf.us> wrote:
>>>>
>>>> I filed bug http://bugs.python.org/issue17853 last night.
>>>>
>>>> If somebody could point me in the right direction (mainly which files to
>>>> look in), I'd be happy to attempt a patch.
>>>
>>> Hmm, interesting challenge. A key part of the problem is that the 3.x
>>> compiler assumes there's no way to inject names it doesn't know about into
>>> code inside a function - we missed the fact that you could still do it with
>>> a nested class and a metaclass __prepare__ method.
>>
>> That's not the problem. You can't inject names dynamically into a
>> function scope with variables in a class scope, since nothing closes
>> over them.
>
> Yeah, what I wrote didn't quite capture what I meant:
>
> - in Python 2.x, using LOAD_DEREF when a nested class body references
> a lexically scoped name is correct
> - in Python 3.x, it is no longer correct, because __prepare__ may
> inject additional names that the compiler doesn't know about

You could still get the same "bug" in Python 2 by messing with
locals() in a class within a function.


--
Regards,
Benjamin


More information about the Python-Dev mailing list