[Types-sig] Re: Meta-classes discussion starter

Barry A. Warsaw bwarsaw@cnri.reston.va.us (Barry A. Warsaw)
Mon, 30 Nov 1998 23:12:51 -0500 (EST)


>>>>> "JvR" == Just van Rossum <just@letterror.com> writes:

    JvR> That could make for some interesting Python 2.0 syntax: class
    JvR> MyClass = MyMetaClass(BaseClass1, ..., BaseClassN): ...etc.
    JvR> where class MyClass(BaseClass1, ..., BaseClassN): ...  would
    JvR> be a shortcut for class MyClass = ClassClass(BaseClass1, ...,
    JvR> BaseClassN): ...etc.  (where ClassClass is the default
    JvR> metaclass)

I haven't seen any comments on my posted syntax proposal; perhaps it's
gross, but anyway I liked it.  Or maybe because it's buried in a long
egroups posting:

    <http://www.egroups.com/list/python-classes/54.html>

Merging with Just's posting, the jist is: if `class' were short hand
for calling ClassClass, and `class' were an object you could put
attributes on, then you could stick metaclasses on the `class' object
and use them in a class definition.  I called the metaclass a `nature' 
so here's how you would create a new metaclass/nature, and then create 
a class using that new nature:

class BeanNature(class):
    pass

class.BeanNature = BeanNature

class.BeanNature BeanieBaby:
    pass

Read near the bottom of the above referenced article for details.

    JvR> 	If the __dict__ of the class that is being built has a
    JvR> __class__ key, call that thing with (name, bases, methods)

    JvR> Which means you specify a metaclass like this:

    | class MyClass:
    | 	__class__ = MyMetaClass

    JvR> Not exactly pretty, but at least it doesn't look like
    JvR> inheritance. It also means that if you want to subclass from
    JvR> MyMetaClass you write

Not exactly ugly either!  Very similar to my proposal except 1) I
chose __nature__ as the magic attribute, and 2) I didn't actually
implement it like you did, just sort of modeled it in Python.

    JvR> This is not theory: I've got it working right here, and I
    JvR> seriously propose it for 1.6, if not 1.5.2. (If anyone wants
    JvR> to play, I could post a patch.)

Yes, please do!

-Barry