customizing metaclass constructed classes


Robin Becker

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

so in the base module
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
class C(A):
_fieldDefs =[

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

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.


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question