Welcome, guest | Sign In | My Account | Store | Cart

This is an extension to the abc.ABCMeta class. It is related to recipe 577711.

Basically it has ABCMeta.register add __implements__ to any subclass that gets registered.

Python, 13 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import abc

class ABCMeta(abc.ABCMeta):
    def register(cls, subclass):
        subclass = super(ABCMeta, cls).register(subclass)
        if not hasattr(subclass, "__implements__"):
            try:
                subclass.__implements__ = {cls}
            except TypeError:
                pass
        else:
            subclass.__implements__.add(cls)
        return subclass

This way a class can know what "interfaces" it implements.