Feature Requests item #1483384, was opened at 2006-05-07 10:41
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1483384&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: None
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Michael Tsai (michaeltsai)
Assigned to: Raymond Hettinger (rhettinger)
Summary: Add set.member() method

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2006-05-08 23:36

Message:
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.

----------------------------------------------------------------------

Comment By: Michael Tsai (michaeltsai)
Date: 2006-05-08 07:07

Message:
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.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2006-05-08 00:37

Message:
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



----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-05-07 15:36

Message:
Logged In: YES 
user_id=849994

Moving to Feature Requests. Note that I do not think Raymond
will agree to this.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1483384&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to