[Python-3000] Metaclasses in Py3K (original) (raw)

Phillip J. Eby pje at telecommunity.com
Fri Dec 8 19:21:54 CET 2006


At 08:53 AM 12/8/2006 -0600, Guido van Rossum wrote:

I've been thinking about this too, and I think it's reasonable to let the metaclass provide the dict to be used as locals. This is easier when the metaclass is given in the class header, e.g. by way of a base class, or through the default metaclass. Although the default default metaclass could of course have a hook to pass things on to another metaclass specified using a metaclass assignment, I'd rather innovate a bit more and add keyword arguments to the class header, e.g.

class C(B1, B2, metaclass=Foo): ...

Did you have any idea for what other keywords besides 'metaclass' might mean? Otherwise, this doesn't seem to add much over, say:

class C(metaclass(Foo), B1, B2):

(Except that metaclass() would then have to be a built-in, I suppose.)

I definitely like the idea of the metaclass getting to control the locals() of the class suite, but then again it also seems like it would be just as easy to say that the class statement creates a class/type object (using the normal metaclass rules applied to the bases), and then the suite executes with a locals() that does getattr(cls, name) and setattr(cls, name, val) for getitem and setitem.

This allows you to do a variety of interesting things, including assigning to class in the body of the class to set (or change!) the metaclass directly, and allowing you to define metaclass-level properties. You can also access inherited methods and attributes in the body of the class (due to the use of getattr()).

With those features in place, I'm not sure what we'd do with class statement keywords, unless we were going to use them to create parameterized types or something, which might be cool, but I have no idea at the moment how to go about doing it. :)



More information about the Python-3000 mailing list