[Python-3000] Metaclasses in Py3K

Ronald Oussoren ronaldoussoren at mac.com
Sat Dec 9 21:01:40 CET 2006


On Dec 9, 2006, at 2:17 AM, Phillip J. Eby wrote:

> At 01:20 PM 12/9/2006 +1300, Greg Ewing wrote:
>> Talin wrote:
>>> In other words, the
>>> __metaclass__ statement would have a side-effect of replacing the
>>> locals() dict with a mapping object supplied by the metaclass.
>>
>> __metaclass__ isn't a statement, it's just an
>> attribute that is interpreted in a special way
>> *after* the class namespace has been populated,
>> by which time it's too late to do what you
>> suggest.
>
> Note that if we simply make locals() set or get attributes on the  
> class
> instance, assigning to __class__ in the body of the class would  
> actually
> set the metaclass immediately.
>
> The principal hurdle that would need to be overcome to do this, is  
> that you
> can't change a builtin type's __class__ currently:

Another hurdle is that you can add slots the the type object  
(currently only in C code), how would your scheme deal with that?

PyObjC implements types that correspond with Objective-C classes and  
add a (hidden) slot to type objects that refers to those classes.  
Classic classes are a simular problem: they aren't instances of type:

 >>> class C: pass
 >>> isinstance(C, type)
False
 >>> isinstance(object, type)
True

I know classic classes are going away, but this does seem to point to  
a problem with your scheme: metaclasses can change the type of the  
object created by a class statement.

Ronald



More information about the Python-3000 mailing list