module wide metaclass for new style classes

Daniel Nogradi nogradi at gmail.com
Sat Dec 16 12:44:42 EST 2006


I used to have the following code to collect all (old style) class
names defined in the current module to a list called reg:


def meta( reg ):
    def _meta( name, bases, dictionary ):
        reg.append( name )
    return _meta

reg = [ ]
__metaclass__ = meta( reg )

class c1:
    pass

class c2:
    pass

print reg


This would correctly print [ 'c1', 'c2' ]. Now I would like to switch
to new style classes but replacing class c1: and class c2: by class
c1(object): and class c2(object): doesn't work because the metaclass
associated with object will be called and not mine. Of course if I
would add __metaclass__ = meta(reg)  to all class definitions that
would work, but how do I do this on the module level?



More information about the Python-list mailing list