Using metaclasses to inherit class variables

telesphore4 at gmail.com telesphore4 at gmail.com
Sun May 21 16:10:11 CEST 2006


If some one ever wants to build on this in the future, the current form
and use is:

import copy

class ClassVars(type):
    classVars = dict(fields=[], longest=0) # <<<< adjust this >>>>
    def __init__(cls, name, bases, dict):
        for name, value in ClassVars.classVars.iteritems():
            if name not in dict: setattr(cls, name, copy.copy(value))

class Table(object):
    __metaclass__ = ClassVars
    # Rest of class follows...

I use  these class varaibles in 2 places and attempted, breifly, to
abstract them into a module. But I ran into a chicken and egg situation
were I wanted to use fresh copies of class variables to define the
abstracted class. Given that I'd onlly save (2*3) 6 lines of code in my
application, I figured that move on for now.




More information about the Python-list mailing list