I've made a highly recursive, hashable (assumed immutable) datastructure. Thus it would be nice to have only one instance of each object (if objectA == objectB
, then there is no reason not to have objectA is objectB
).
I have tried solving it by defining a custom __new__()
. It creates the requested object, then checks if it is in a dictionary (stored as a class variable). The object is added to the dict if necessary and then returned. If it is already in the dict, the version in the dict is returned and the newly created instance passes out of scope.
This solution works, but
- I have to have a dict where the
value
at eachkey
is the same object. What I really need is to extract an object from a set when I "show" the set an equal object. Is there a more elegant way of doing this? - Is there a builtin/canonical solution to my problem in Python? Such as a class I can inherit from or something....
My current implementation is along these lines:
class NoDuplicates(object):
pool = dict()
def __new__(cls, *args):
new_instance = object.__new__(cls)
new_instance.__init__(*args)
if new_instance in cls.pool:
return cls.pool[new_instance]
else:
cls.pool[new_instance] = new_instance
return new_instance
I am not a programmer by profession, so I suspect this corresponds to some well known technique or concept. The most similar concepts that come to mind are memoization and singleton.
One subtle problem with the above implementation is that __init__
is always called on the return value from __new__
. I made a metaclass to modify this behaviour. But that ended up causing a lot of trouble since NoDuplicates
also inherits from dict
.