Licheng Fang a écrit :
> I mean, all the class instances that equal to each other should be
> reduced into only one instance, which means for instances of this
> class there's no difference between a is b and a==b.
Here's a Q&D attempt - without any garantee, and to be taylored to your
needs.
_values = {} #id(instance) => value mapping
_instances = {} #hash(value) => instance mapping
class Value(object):
def __new__(cls, value):
try:
return _instances[hash(value)]
except KeyError:
instance = object.__new__(cls)
_values[id(instance)] = value
_instances[hash(value)] = instance
return instance
@apply
def value():
def fget(self):
return _values[id(self)]
def fset(self, ignore):
raise AttributeError("%s.value is read only" % type(self))
def fdel(self):
raise AttributeError("%s.value is read only" % type(self))
return property(**locals())
HTH
--
http://mail.python.org/mailman/listinfo/python-list