customizing metaclass constructed classes

Discussion in 'Python' started by Robin Becker, Feb 15, 2005.

  1. Robin Becker

    Robin Becker Guest

    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
    Robin Becker, Feb 15, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bengt Richter

    decorating classes with metaclass

    Bengt Richter, Mar 14, 2005, in forum: Python
    Replies:
    1
    Views:
    347
    Simon Percivall
    Mar 15, 2005
  2. ironfroggy
    Replies:
    16
    Views:
    430
    Michele Simionato
    Jun 3, 2005
  3. Daniel Nogradi

    module wide metaclass for new style classes

    Daniel Nogradi, Dec 16, 2006, in forum: Python
    Replies:
    3
    Views:
    269
    Daniel Nogradi
    Dec 17, 2006
  4. aaragon
    Replies:
    12
    Views:
    1,296
    aaragon
    Nov 8, 2006
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    299
Loading...

Share This Page