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 id

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, http://code.activestate.com/reci

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

2009-04-03 Thread Nick Coghlan
Paul Moore wrote: > 2009/4/3 R. David Murray : >> 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 q

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 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 > do this? >>> que

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

2009-04-03 Thread Paul Moore
2009/4/3 R. David Murray : > 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 misread the sec

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 Alexander Belopolsky
I just want to add a link to a 2.5 year old discussion on this issue: . In that discussion I disagreed with Martin and argued that "interning is a set operation and it is unfortunate that set API does not support it directly." On Fri, Apr 3, 2009 at 12:43 PM

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

2009-04-03 Thread Raymond Hettinger
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/ Raymond ___ Python-Dev ma

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

2009-04-03 Thread Paul Moore
2009/4/3 Steven D'Aprano : > 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 sense :-) Martin

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 Amaury Forgeot d'Arc
Hi, On Fri, Apr 3, 2009 at 17:45, Sebastian Rittau wrote: > I am missing a simple way to retrieve the "first" element of any > iterable in python that matches a certain condition anyway. Something > like this: > >  def first(iter, cb): >      for el in iter: >          if cb(el): >              r

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

2009-04-03 Thread Sebastian Rittau
Hello, On Fri, Apr 03, 2009 at 02:07:02PM +0200, Hrvoje Niksic 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 >

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 Antoine Pitrou
Steven D'Aprano pearwood.info> writes: > > 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 "", line 1, in > TypeError: list indices must be integers Your example is wrong: >>> alist = [0,

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 ali

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 tha

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

2009-04-03 Thread Paul Moore
2009/4/3 Hrvoje Niksic : > 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 of objects identi

[Python-Dev] Getting values stored inside sets

2009-04-03 Thread Hrvoje Niksic
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 of objects identified by some piece of informatio