customizing metaclass constructed classes

Robin Becker robin at reportlab.com
Tue Feb 15 13:43:54 EST 2005


Robert Brewer wrote:
.......
..........
> 
> You *are* using a language with callable functions, right? ;)
> 
> #base.py
> 
> def create_special_property(cls, f):
>    # create special property based on f
> 
> class M(type):
>     def __init__(cls, name, bases, dict):
>        super(M, cls).__init__(name, bases, dict)
>        for f in dict['_fieldDefs']:
>            create_special_property(cls, f)
> 
> #database.py
> class C(A):
>    _fieldDefs =[
>      IntColumn('id',primaryKey=1,allowNull=0),
>      TextColumn('name',allowNull=0,size=50),
>      TextColumn('description',allowNull=1,size=50),
>      ]
> 
> base.create_special_property(C, DateColumn('start_date'))

You're absolutely right :) I can modify the class in place if I
have access to the required modifications.

This seems more easily assimilable and definitely works in my cases.
Would it make sense to put the def create_special_property inside the metaclass 
as it then becomes available as a class method of A as well as being visible as 
cls.create_special_property(f) in the metaclass __init__?

> Slightly off-topic: if you really want to make it pretty, add a custom
> descriptor so you can write:
> 
> class C(A):
>    id = IntColumn(primaryKey=1,allowNull=0)
>    name = TextColumn(allowNull=0,size=50)
>    description = TextColumn(allowNull=1,size=50)
> 

I'll certainly take a look at this

> See UnitProperty (the descriptor), MetaUnit (the metaclass), and Unit
> (the domain object) in http://www.aminus.org/rbre/dejavu/__init__.py for
> an implementation of mine which you can steal in toto (because it's
> public domain).
> 

-- 
Robin Becker




More information about the Python-list mailing list