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__(name, 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,allowNull=0),
    TextColumn('name',allowNull=0,size=50),
    TextColumn('description',allowNull=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.append(....)

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._fieldDefs+[......]
    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


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to