Extends /

__all__ = ['Base', 'Extend']

def get_extends(cls):
    # __subclasses__ is sorted in loading order and the mro
    # executes the bases left-to-right. We want the resolution
    # order of the extended type to be last-loaded first (the
    # latest-loaded extender sees its methods executed first,
    # execution goes up the extension sequence and ends on the
    # base class), therefore we must revert ``extenders``
    return [
        sub for sub in cls.__subclasses__()
        if issubclass(sub, Extend)
        if sub is not Extend
        if cls._accepts(sub)][::-1]

class MetaBase(type):
    def __new__(typ, name, bases, attributes):
        return type.__new__(
            typ, name, bases,
            dict(attributes, _extension_filters=[]))

class Extend(object):

class Base(object):
    __metaclass__ = MetaBase
    def __new__(cls):
        base_extenders = get_extends(cls)
        ancestor_extenders = sum(
            [get_extends(ancestor) for ancestor in cls.mro()[1:]
             if issubclass(ancestor, Base)
             if ancestor is not Base], [])
        if not (base_extenders or ancestor_extenders):
            return object.__new__(cls)

        extenders = (base_extenders or [cls]) + ancestor_extenders
        return object.__new__(
            MetaBase(cls.__name__, tuple(extenders), {}))
    def add_filter(cls, callable):
    def _accepts(cls, candidate):
        for class_ in cls.mro():
            for callable in getattr(class_, '_extension_filters', []):
                if callable(cls, candidate) is False:
                    return False
        return True