customizing metaclass constructed classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robin Becker

    #1

    customizing metaclass constructed classes

    I have a set of classes for database access which are constructed using a
    special metaclass and base class.

    so in the base module

    #base.py
    class M(type):
    def __init__(cls, name, bases, dict):
    super(M, cls).__init__(n ame, bases, dict)
    for f in dict['_fieldDefs']:
    #create special property based on the field def
    ....

    class A(object):
    __metaclass__ = M
    _fieldDefs = []

    #base methods
    ......

    in the database module we use the above
    eg

    #database.py
    class C(A):
    _fieldDefs =[
    IntColumn('id', primaryKey=1,al lowNull=0),
    TextColumn('nam e',allowNull=0, size=50),
    TextColumn('des cription',allow Null=1,size=50) ,
    ]

    Now to my question: can I change the definition of the client class C at run
    time? I want to do something like add another element to C's _fieldDefs and then
    get it to go through the construction phase provided by M. It's clearly
    pointless for me to try something like

    from database import C
    C._fieldDefs.ap pend(....)

    as C is constructed by the import.
    I either have to delay C's construction or reconstruct it.

    Would something like this do?

    def reconstruct():
    import database
    class C(database.C):
    _fieldDefs = database.C._fie ldDefs+[......]
    database.C = C

    Is there a better way to do this which preserves more of C's original identity?
    I suppose I want to call the metaclass initialization again, but can't see a way
    to do that.
    --
    Robin Becker

Working...