another question on metaclasses

Mike Dean klaatu at evertek.net
Tue Dec 10 20:31:52 EST 2002


On 10 Dec 2002 14:55:42 -0800 Michele Simionato <mis6 at pitt.edu> wrote:
> But still I haven't seen a compelling example in comp.lang.python (at
> least, one that I could understand ;-)

Disclaimer:  I, too, haven't been able to use metaclasses practically
myself - mainly just played with how they work, and was amazed at their
versatility (and the things you can do _without_ them).

But, I can envision one immensly practical application -
object-relational mappers.  Say you have a metaclass, DBSerializable.
This metaclass takes the classes attributes, which are supposed to hold
sentinel objects representing data types, and generates a class with all
the database serialization code automatically.  So you can do:

class Client:
    __metaclass__ = DBSerializable
    
    id = DB_Int
    name = DB_Str(50)
    sales_rep_id = DB_Int(link=SalesRep.id)

and the metaclass wrapps everything in properties that automatically
handle database stuff, making everything smooth and transparent.  Also,
maybe another metaclass makes the object model network-transparent, etc.

Hmm, I wonder if something like MiddleKit will ever adopt such an
idea...

-Mike



More information about the Python-list mailing list