No title Revision 363730643234 (Fri Nov 18 2011 at 14:20) - Diff Link to this snippet: https://friendpaste.com/1CCqZOLR7Jog7kDLe0gJrD Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 1234567891011121314151617181920212223242526272829303132333435363738# -*- coding: utf-8 -*-_instances = {}class Wrapper(object): def __new__(cls, **kwargs): instance = object.__new__(cls) instance.__init__(**kwargs) if instance.uuid in _instances: cached = _instances[instance.uuid] for attr in kwargs.items(): setattr(cached, *attr) return cached else: _instances[instance.uuid] = instance return instance @property def uuid(self): raise NotImplementedError()class Test(Wrapper): def __init__(self, name=None): self.name = name @property def uuid(self): return self.nameid(Test(name='toto')) == Test(name='toto')t1 = Test(name='toto')t2 = Test(name='toto')print id(t1) == id(t2)t1.name = 'tata'print t2.name