Understanding and working with __metaclass__

  • Thread starter David M. Wilson
  • Start date
D

David M. Wilson

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.
 
M

Mike C. Fletcher

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

Is intended to answer exactly this question.

HTH,
Mike
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
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
M

Michele Simionato

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top