

class IdentifiedMetaClass(type):
    def __new__(meta, name, bases, nmspc):
        nmspc['__cache__'] = {}
        # need to manipulate __init__ so that it isn't called twice
        # when we retrieve from the cache, but how to avoid inheritance
        return super().__new__(meta, name, bases, nmspc)

class IdentifiedBase(object, metaclass=IdentifiedMetaClass):
    def __new__(cls, *args):
        instance = cls.__cache__.get(args, None)
        if args:
            return instance
        instance = super().__new__(cls)
        cls.__cache__[args] = instance
        return instance


class TrialIdentified(IdentifiedBase):
    def __init__(self, *args):
        assert not hasattr(self, 'done')
        self.done = True

    def foo(self):
        pass

def test_identified():
    x = TrialIdentified('fish')
    y = TrialIdentified('fish')

    assert x is y



@@ -305,82 +305,22 @@
 c)%0A%0A
-class IdentifiedBase(object, metaclass=IdentifiedMetaClass):%0A
+ 
    
- 
 def __
-new
+call
 __(c
@@ -391,20 +391,24 @@
      if 
-args
+instance
 :%0A      
@@ -462,17 +462,25 @@
 ).__
-new
+call
 __(cls
+, *args
 )%0A  
@@ -541,16 +541,86 @@
 stance%0A%0A
+class IdentifiedBase(object, metaclass=IdentifiedMetaClass):%0A    pass%0A
 %0Aclass T
@@ -870,16 +870,16 @@
 fish')%0A%0A
-
     asse
@@ -888,10 +888,63 @@
  x is y%0A
+    assert x is not TrialIdentified('fish', 'chaps')%0A
 %0A%0A

use metaclass __call__ to reproduce cached objects