[Python-3000] Metaclasses in Py3K

Phillip J. Eby pje at telecommunity.com
Sat Dec 9 02:17:38 CET 2006


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:

Python 2.5 (r25:51908, Dec  6 2006, 15:13:26) [MSC v.1310 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> class X(object): pass
...
 >>> class Y(type): pass
...
 >>> X.__class__ = Y
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: only for heap types

Fixing this problem in Python 2.6 would be nice, actually, to say nothing 
of Py3K.



More information about the Python-3000 mailing list