Re: [Python-Dev] Getting values stored inside sets

2009-04-06 Thread Hrvoje Niksic
Raymond Hettinger wrote: Hrvoje Niksic wrote: I've stumbled upon an oddity using sets. It's trivial to test if a value is in the set, but it appears to be impossible to retrieve a stored value, See: http://code.activestate.com/recipes/499299/ Thanks, this is *really* good, the kind of

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Paul Moore
2009/4/3 Hrvoje Niksic hrvoje.nik...@avl.com: I've stumbled upon an oddity using sets.  It's trivial to test if a value is in the set, but it appears to be impossible to retrieve a stored value, other than by iterating over the whole set.  Let me describe a concrete use case. Imagine a set

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Terry Reedy
Hrvoje Niksic wrote: I've stumbled upon an oddity using sets. It's trivial to test if a value is in the set, but it appears to be impossible to retrieve a stored value, Set elements, by definition, do not have keys or position by which to grab. When they do, use a dict or list. other

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Steven D'Aprano
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

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Steven D'Aprano
On Sat, 4 Apr 2009 02:07:28 am Antoine Pitrou wrote: Your example is wrong: Of course it is. The perils of posting at 2am, sorry. Nevertheless, the principle still holds. There's nothing in Python that prohibits two objects from being equal, but without them being interchangeable. As poorly

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Martin v. Löwis
I've stumbled upon an oddity using sets. It's trivial to test if a value is in the set, but it appears to be impossible to retrieve a stored value, other than by iterating over the whole set. Of course it is. That's why it is called a set: it's an unordered collection of objects, keyed by

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Paul Moore
2009/4/3 Steven D'Aprano st...@pearwood.info: Python does not promise that if x == y, you can use y anywhere you can use x. Nor should it. Paul's declaration of abuse of __eq__ is unfounded. Sorry, I was trying to simplify what I was saying, and simplified it to the point where it didn't make

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread R. David Murray
On Fri, 3 Apr 2009 at 17:57, Paul Moore wrote: In fact, Python seems to be doing something I don't understand: class Element(object): ...def __init__(self, key, id): ...self.key = key ...self.id = id ...def __eq__(self, other): ...print Calling __eq__ for %s %

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Paul Moore
2009/4/3 R. David Murray rdmur...@bitdance.com: a == b So, python calls a.__eq__(b) Now, that function does: a.key == b Since b is an object with an __eq__ method, python calls b.__eq__(a.key). That's the bit I can't actually find documented anywhere. Ah, looking again I see that I

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Leif Walsh
On Fri, Apr 3, 2009 at 8:07 AM, Hrvoje Niksic hrvoje.nik...@avl.com wrote: But I can't seem to find a way to retrieve the element corresponding to 'foo', at least not without iterating over the entire set.  Is this an oversight or an intentional feature?  Or am I just missing an obvious way to

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Nick Coghlan
Paul Moore wrote: 2009/4/3 R. David Murray rdmur...@bitdance.com: a == b So, python calls a.__eq__(b) Now, that function does: a.key == b Since b is an object with an __eq__ method, python calls b.__eq__(a.key). That's the bit I can't actually find documented anywhere. It doesn't

Re: [Python-Dev] Getting values stored inside sets

2009-04-03 Thread Raymond Hettinger
[Nick Coghlan] It doesn't quite work the way RDM desribed it - he missed a step. Thanks for the clarification. We ought to write-out the process somewhere in a FAQ. It may also be instructive to step through the recipe that answers the OP's original request,