[Tutor] What is the metaclass for?

alan.gauld@bt.com alan.gauld@bt.com
Thu Mar 6 12:22:05 2003


> > I've been reading something about metaclass but most of the
> > documents I read so far are more or less like Martian codes to
> My short answer would be that metaclasses are a mechanism for black
> magic -- or at least, very dark grey magic. ;) Odds are good that if
> what you read about metaclasses makes no sense, then you're not likely
> to need metaclasses for anything that you're doing 

I'd second that, but I'll have a go anyway...

Metaclasses do slightly different things in different languages.
Basically they can variously be thought of as factory classes, 
a home for class methods(ie ones which act on all instances of 
a class) or class generators(somewhat like higher order functions 
- which return other functions as a result).

One example might be a meta string class from which we can instantiate
multiple different string type classes. For example one string will act as 
normal, another converts all to lowercase, another to upper case etc.
All of this ontrolled by a constructor parameter...

Other things become too much like black magic to explain here, but 
there is a hard to find Metaclass book that goes into the darker corners...

Alan G.