Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Understanding and working with __metaclass__

0 views
Skip to first unread message

David M. Wilson

unread,
Dec 8, 2003, 2:50:00 PM12/8/03
to
Hi there,

I've been sitting on the __metaclass__ feature for about 2 weeks now,
and haven't really got that far with it. It looks like a very powerful
tool, and I think I have even already identified places in my code
where I could use it quite effectively, but I can't quite tell what it
is /meant/ to be used for, or indeed what it is popularly used for.

Is there a page, posting, or other place I can turn to to get a
developer's run-down on the practical uses of __metaclass__? The
Python documentation states very clearly what it does, but I fear that
journeying off on my own with it will lead to some terrible habit
formation, and I already have enough of those to deal with. :) (:set
ts=3 ;)

Thanks for your time,


David.

Mike C. Fletcher

unread,
Dec 8, 2003, 4:18:32 PM12/8/03
to David M. Wilson, pytho...@python.org
http://members.rogers.com/mcfletch/programming/metaclasses.pdf

Is intended to answer exactly this question.

HTH,
Mike

David M. Wilson wrote:

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


David M. Wilson

unread,
Dec 9, 2003, 7:42:16 AM12/9/03
to
"Mike C. Fletcher" <mcfl...@rogers.com> wrote...

> http://members.rogers.com/mcfletch/programming/metaclasses.pdf

> Is intended to answer exactly this question.

Thanks, this is _very much_ appreciated and exactly what I was after.
The "Quickie Definitions" slide probably deserves a print-out and
getting stuck on my wall for the next week or two until it properly
sinks in. :)

Thanks again,


David.

Maxim Khesin

unread,
Dec 9, 2003, 12:55:47 PM12/9/03
to
There is a nice example in from mailman by Eric Raymond:
http://pythonology.org/success&story=esr

Michele Simionato

unread,
Dec 9, 2003, 2:19:22 PM12/9/03
to
dw-goo...@botanicus.net (David M. Wilson) wrote in message news:<99dce321.03120...@posting.google.com>...

Uhm ... this question has been asked here many times and I am beginning
to think it should be in the FAQ. Any volunteer for a tentative FAQ entry?

I must say that I don't have a precise opinion on the subject yet, since
in most cases you can avoid metaclasses without loosing any essential
functionality. Metaclasses are somewhat elegant, but you pay the elegance
with impliciteness and we all know that "esplicit is better than implicit".

A typical use case for metaclasses is when you have a hierarchy
written to do X, and you want it to do Y. Instead of rewriting all
the classes, a quick and dirty metaclass can give you the desired
functionality with a little effort.

Also, you can use metaclasses to debug classes.

Metaclasses allow to implement Aspect Oriented Programming for Python,
and there are already modules doing that.

With metaclasses you can modify the semantics of the language.
For instance, you could design a declarative language on top of Python.
Nevertheless, IMO these tricks don't fit well with the spirit of the
language, so even if you can do that, I am not convinced you should do
that.

A reasonable application of metaclasses is in the writing of plugins:
you may import at runtime the same module but with different metaclasses,
changing its behaviour according to the external conditions.

See for instance

http://www-106.ibm.com/developerworks/library/l-pymeta.html

for more on this idea.

Google for "Guido metaclasses a.k.a. the killer joke" for additional
motivations. The standard distributions contains various examples
of metaclasses.

My advice: use a lot of metaclasses for experimental purposes and
playing, very little (better zero) for production code.

The fact that you can correct your design mistakes with metaclasses does
not mean that you should not correct them by rewriting you classes
from scratch ;)


Michele

0 new messages