Generic metaclass to keep track of subclasses?

前端 未结 3 1228
生来不讨喜
生来不讨喜 2021-02-20 05:26

I\'m trying to write a generic metaclass to track subclasses

Since I want this to be generic, I didn\'t want to hardcode any class name within this metaclass, therefore

3条回答
  •  感情败类
    2021-02-20 05:56

    Here's something I've been playing around with (that works):

    def sublass_registry():
        ''' Create a metaclass to register subclasses '''
    
        class SublassRegistryMeta(type):
            def __init__(cls, name, bases, classdict):
                if classdict.get('__metaclass__') is SublassRegistryMeta:
                    SublassRegistryMeta.lineage = [cls] # put root class at head of a list
                else:
                    # sublclasses won't have __metaclass__ explicitly set to this class
                    # we know they're subclassees because this ctor is being called for them
                    SublassRegistryMeta.lineage.append(cls) # add subclass to list
                type.__init__(cls, name, bases, classdict)
    
        return SublassRegistryMeta
    
    def subclasses(cls):
        ''' Return a list containing base and subclasses '''
    
        try:
            if cls.__metaclass__.lineage[0] is cls: # only valid for a root class
                return cls.__metaclass__.lineage
        except AttributeError:
            pass
        return None
    
    class Car(object): # root class
        __metaclass__ = sublass_registry()
    
    class Audi(Car): # inherits __metaclass__
        pass
    
    class Ford(Car): # inherits __metaclass__
        pass
    
    class Audi2(Audi): # sub-subclass also inherits __metaclass__
        pass
    
    print subclasses(Car)
    # [, , , ]
    print subclasses(Audi)
    # None
    

提交回复
热议问题