Martin Teichmann added the comment:

Currently, a class is created as follows: the compiler turns the class 
statement into a call to __build_class__. This runs the class body. If 
__class__ or super() is used within a method of the class, an empty PyCell is 
created, to be filled later with the class once its done.

The class body returns this cell. Then the metaclass is called to create the 
actual class, and finally the cell is set to whatever the metaclass returns.

This has the disadvantage that in the metaclasses __new__ and __init__, 
__class__ and super() are not set. This is a pity, especially because the two 
parameter version of super() doesn't work either, as the class is not yet bound 
to a name.

The attached patch lets the compiler add said cell as __classcell__ to the 
classes namespace, where it will later be taken out by type.__new__ in order to 
be properly filled.

This resembles the approach used for __qualname__, with the difference that 
__qualname__ is already added at the beginning of the classes body, such that 
it is visible to the user.

This way __class__ will be properly set immediately after it is created, thus 
all methods are immediately usable, already in a metaclasses __new__ or 
__init__.

This changes the behavior if a metaclass returns another class. currently, 
__build_class__ will try to set the __class__ in the methods of the class body 
to whatever __new__ returns, which might be completely unrelated to the classes 
body.

----------
keywords: +patch
Added file: http://bugs.python.org/file43765/pep487.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23722>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to