views:

203

answers:

5

I'm learning about metaclasses in Python. I think it is a very powerful technique, and I'm looking for good uses for them. I'd like some feedback of good useful real-world examples of using metaclasses. I'm not looking for example code on how to write a metaclass (there are plenty examples of useless metaclasses out there), but real examples where you have applied the technique and it was really the appropriate solution. The rule is: no theoretical possibilities, but metaclasses at work in a real application.

I'll start with the one example I know:

  • Django models, for declarative programming, where the base class Model uses a metaclass to fill the model objects of useful ORM functionality from the attribute definitions.

Looking forward to your contributions.

A: 

SQLalchemy also uses them for declarative database models.

Sorry my answer isn't very different from your example, but if you're looking for example code, I found declarative to be pretty readable.

jcdyer
+2  A: 

In Python 2.6 and 3.1, the Python standard library provides an abc.ABCMeta, a meta-class for Abstract Base Classes ("ABCs"). Classes that use the meta-class can use @abstractmethod and @abstractproperty to define abstract methods and properties. The meta-class will ensure that derived classes override the abstract methods and properties.

Also, classes that implement the ABC without actually inheriting from it can register as implementing the interface, so that issubclass and isinstance will work.

For example, the collections module defines the Sequence ABC. It also calls Sequence.register(tuple) to register the built-in tuple type as a Sequence, even though tuple does not actually inherit from Sequence.

Daniel Stutzbach
A: 

The only time I used a metaclass so far was to write a deprecation warning mechanism. It was something along the following lines - syntax may be very approximative, but code will illustrate my point more easily than a complicated sentence :

class New(object):
  pass

class Old(object):
  def __new__(self):
    deprecation_warning("Old class is no more supported, use New class instead")
    return New()
rotoglup
+2  A: 

FormEncode validators and Turbogears / Tosca widgets.

You might also be interested in class decorators: they can be written with the latest releases, and cover many use cases that were previously handled with metaclasses.

Marco Mariani
+2  A: 

The Python implementation of Protocol Buffers uses metaclasses to generate the Python bindings that represent your data format. From the tutorial:

The important line in each class is __metaclass__ = reflection.GeneratedProtocolMessageType. While the details of how Python metaclasses work is beyond the scope of this tutorial, you can think of them as like a template for creating classes. At load time, the GeneratedProtocolMessageType metaclass uses the specified descriptors to create all the Python methods you need to work with each message type and adds them to the relevant classes. You can then use the fully-populated classes in your code.

ire_and_curses