Another way to spell __metaclass__ ?

Martin v. Löwis martin at v.loewis.de
Fri Dec 13 10:42:11 EST 2002


> I must confess there is something that bothers me about __metaclass__
> as an impementation of metaclass functionality. It strikes me more as
a
> class transmogrification hook with a debug/trace flavor than a clear
> expression of the relationship between two classes, one of which
> produces the other as an instance.

Right. All those __ names in new-style classes (slots, metaclass), as
well as the builtins (classmethod, staticmethod, property) aren't really
appropriate as a notation of the features they implement. Instead, there
is a long-standing plan to provide "proper syntax" for these. It appears
that this won't happen for Python 2.3, either, but contributions in that
area are welcome, both as proposals for syntax, or, better yet, as
implementations of this syntax.

The issue with metaclasses is that the class keyword is nearly but not
completely correct as a notation for metaclass-derived classes.
Originally, it only creates classes from the "class" metaclass, with
newstyle classes it can also create classes from the "type" metaclass;
this is determined in a straight-forward way by looking at the base
classes. If you want to use a different metaclass, there ought to be
some annotation of the class declaration. For example, if the class
really belongs to the metaclass "interface", then writing

class IUnknown:
    __metaclass__ = interface

is not appropriate; it would be much more natural to write

interface IUnknown:
    pass

However, adding new keywords dynamically to the language is not good
either, as it complicates parsing (both for the human reader, and for
the machine reader). One strawman proposal would be to write

interface class IUnknown:
    pass

where an optional expression precedes the keyword class; this
expression's value is then the metaclass. Clearly, you'd have to do

from COM import interface

before that; you could also write

import COM
COM.interface class IUnknown:
    pass

There are certainly other ways to write this down; I don't know what the
most intuitive and least hacky way is.

Regards,
Martin




More information about the Python-list mailing list