@classmethod
def _get_instance(cls, *args, **kargs):
    return cls.__instances.setdefault(
                                (args, tuple(kargs.items())),
                                super(type(cls), cls).__new__(*args, **kargs))


def flyweight(decoree):
    decoree.__instances = dict()
    decoree.__new__ = _get_instance
    return decoree


#----------------------------------------------------------
@flyweight
class Spam(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b


@flyweight
class Egg(object):
    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)
