On Fri, 3 Apr 2009 11:22:02 pm Paul Moore wrote:
> I'd say that you're abusing __eq__ here. If you can say "x in s"
> and then can't use x as if it were the actual item inserted into
> s, then are they really "equal"?
That's hardly unusual in Python.
>>> alist = [0, 1, 2, 3, 4]
>>> 3.0 in alist
True
>>> alist[3.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
Besides, there's a concrete use-case for retrieving the actual object
inside the set. You can ensure that you only have one instance of any
object with a particular value by using a cache like this:
_cache = {}
def cache(obj):
if obj in _cache: return _cache[obj]
_cache[obj] = obj
return obj
Arguably, it would be neater if the cache was a set rather than a dict,
thus saving one pointer per item, but of course that would rely on a
change on set behaviour.
--
Steven D'Aprano
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com