This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add set.member() method
Type: enhancement Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: georg.brandl, michaeltsai, rhettinger
Priority: normal Keywords:

Created on 2006-05-07 15:41 by michaeltsai, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg54797 - (view) Author: Michael Tsai (michaeltsai) Date: 2006-05-07 15:41
Right now, when I check membership in a set, the __in__ method just 
returns True/False if there is an object in the set that's == to the 
argument. I would like to have a member() method that returns the object 
in the set or raises KeyError if the argument is not in the set. This would 
be useful for interning and other cases where right now I'd use a 
degenerate dictionary where the keys and values are equal.
msg54798 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-05-07 20:36
Logged In: YES 
user_id=849994

Moving to Feature Requests. Note that I do not think Raymond
will agree to this.
msg54799 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-05-08 05:37
Logged In: YES 
user_id=80475

I'm curious to see some of your dictionary examples that 
do not seem to tranlate cleanly with the existing set API.

In published code, I've not seen people writing anything 
like what is being requested, i.e. I haven't seen 
fragments like:
   if x in s:
      return x
   else:
      raise KeyError

msg54800 - (view) Author: Michael Tsai (michaeltsai) Date: 2006-05-08 12:07
Logged In: YES 
user_id=817528

Well, the example you wrote doesn't capture the pattern I was thinking of. I'm 
thinking of cases like:

d = {}
#...
x = d.setDefault(x, x)

where I want to get the canonical x. This might be x itself, or it might be 
another object that's ==. I doubt you've seen set code that does this with 
sets, because I don't think it's possible with the existing set API to do this 
efficiently.
msg54801 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-05-09 04:36
Logged In: YES 
user_id=80475

The use case is interesting and points-out that the set 
API provides no way to retrieve the exact set member that 
matches an equality comparison; however, that is 
essentially a description of a mapping operation, not a 
set operation.  The x=d.setdefault(x,x) approach 
satisfactorily fulfills the use case (though with a bit 
more memory consumption for the value that duplicates the 
key).

Also, I'm disinclined to introduce such a method to the 
set API which is made simple by a uniform focus on 
equality comparisons and intuitive operations.  Adding an 
identity focused method makes the whole API harder to 
understand and less of a power tool suitable for 
beginners. IOW, I expect that the method would harm more 
than it would help.

I'm rejecting the feature request but want to thank you 
for the thought provoking idea.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43328
2006-05-07 15:41:05michaeltsaicreate