class FlyweightMixin(object):
    _instances = dict()
    def __init__(self, *args, **kargs):
        raise NotImplementedException

    def __new__(cls, *args, **kargs):
        return cls._instances.setdefault(
                    (cls, args, tuple(kargs.items())),
                    super(type(cls), cls).__new__(cls, *args, **kargs))


#----------------------------------------------------------
class Spam(FlyweightMixin):

    def __init__(self, a, b):
        self.a = a
        self.b = b


class Egg(FlyweightMixin):

    def __init__(self, x, y):
        self.x = x
        self.y = y


assert Spam(1, 2) is Spam(1, 2)
assert Egg('a', 'b') is Egg('a', 'b')
assert Spam(1, 2) is not Egg(1, 2)

# Subclassing a flyweight class
class SubSpam(Spam):
    pass

assert SubSpam(1,2) is SubSpam(1,2)
assert Spam(1,2) is not SubSpam(1,2)
