This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients Arfrever, Mark.Shannon, alex, barry, benjamin.peterson, cvrebert, daniel.urban, meador.inge, michael.foord, ncoghlan, python-dev
Date 2012-09-07.10:49:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1347014947.73.0.110387541799.issue12370@psf.upfronthosting.co.za>
In-reply-to
Content
OK, I think I have a way to fix this that will actually *reduce* the level of special casing needed in the compiler.

Specifically, I think we may be able to make the class statement emit *two* scopes, rather than the current one. The outer scope would be created with MAKE_CLOSURE, and thus names defined there would participate in lexical scoping. The inner scope would use the current MAKE_FUNCTION behaviour, and thus names defined there would be ignored by lexical scoping. "__class__" would then be defined in the outer scope *after* the class has been created. It would roughly like the following, except with __qualname__ still set correctly:

>>> def outer():
...     class inner:
...         def method(self):
...             print(the_class)
...     the_class = inner
...     return inner
...
>>> cls = outer()
>>> cls.method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: method() missing 1 required positional argument: 'self'
>>> cls().method()
<class '__main__.outer.<locals>.inner'>

The one potential wrinkle I see is whether this might cause a semantic change for name references from the class body to a containing function, but I think the technique is at least worth trying.

If this works, __class__ would just become a completely ordinary cell reference, and override it at class scope should work again. super() itself would still need magic to handle the automatic lookup of the first positional argument to the calling function, but at least the symtable magic should be able to go away.
History
Date User Action Args
2012-09-07 10:49:07ncoghlansetrecipients: + ncoghlan, barry, benjamin.peterson, Arfrever, alex, michael.foord, cvrebert, meador.inge, daniel.urban, Mark.Shannon, python-dev
2012-09-07 10:49:07ncoghlansetmessageid: <1347014947.73.0.110387541799.issue12370@psf.upfronthosting.co.za>
2012-09-07 10:49:07ncoghlanlinkissue12370 messages
2012-09-07 10:49:06ncoghlancreate