[Python-3000] Class Decorators + ctypes (Was: yes to class decorators)
Nick Coghlan
ncoghlan at gmail.com
Fri Nov 17 13:35:06 CET 2006
Talin wrote:
> Greg Ewing wrote:
>> Talin wrote:
>>> A different approach would be to say that the class decorator can
>>> overload the '__setattr__' method on the class itself during class
>>> creation:
>> But it doesn't work that way -- class attributes don't
>> go through __setattr__ during class creation. They go
>> into a dict that is passed to the type object's
>> constructor. The class doesn't even exist while the
>> class body is being evaluated.
>
> I see that as a shortcoming of the current metaclass implementation. It
> would be much more useful IMHO if the the metaclass had access to the
> declaration order. Perhaps the class decorator protocol could somehow
> take this into account?
Not really, no: currently, no code other than the compiler has access to the
order of declaration of class attributes (and the compiler only has access
because it executes the code to populate that namespace).
The only way I could see it working is to replace the dictionary used during
class instantiation with a dict subclass that kept track of the order that
keys were added to the namespace (for example, storing a list of keys in an
"ordered_keys" attribute).
Adding new keys to and deleting keys from such a dict would be slower than
usual, though (as it would have to update the key list as well as the hash map).
However, actually doing anything like this would require some mechanism for
the metaclass to get class instantiation to use that dict subtype in the first
place...
Regardless, as Jack pointed out: this has *nothing* to do with class
decorators, which operate only on fully constructed classes.
Regards,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list