<div><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_quote">On Wed, May 16, 2012 at 12:40 AM, Mike Meyer <span dir="ltr"><<a href="mailto:mwm@mired.org" target="_blank">mwm@mired.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Wed, 16 May 2012 00:02:31 -0700<br>
Bruce Leban <<a href="mailto:bruce@leapyear.org">bruce@leapyear.org</a>> wrote:<br></div><div class="im">
</div></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">> Here's one definition of get:<br>
> def get_from_set1(s):<br>
> """Return an arbitrary member of a set."""<br>
> return min(s, key=hash)<br>
<br>
</div>>From poking around, at least at one time the fastest implementation<br>
was the very confusing:<br>
<br>
def get_from_set(s):<br>
for x in s:<br>
return x<br></blockquote><div><br></div><div>I didn't claim it was fast. I actually wrote that version instead of the in/return version for a very specific reason: it always returns the same element. (The for/in/return version might return the same element every time too but it's not guaranteed.) </div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> How is this useful?<br>
<br>
Basically, anytime you want to examine an arbitrary element of a set,<br>
and would use pop, except you need to preserve the set for future<br>
use. In my case, I'm running a series of tests on the set, and some<br>
tests need an element.<br>
<br></blockquote><div>That's bordering on tautological. It's useful anytime you need it. I don't think your test is very good if it uses the get I wrote above. Your test will only operate on one element of the set and it's easy to write functions which succeed for some elements of the set and fail for others. I'd like to see an actual test that you think needs this that would not be improved by iterating over the list.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Again, looking for a reason for this not existing turned up other<br>
cases where people were wondering how to do this.<br></blockquote><div><br></div><div>Things are added to APIs and libraries because they are useful, not because people wonder why they aren't there. set.get as you propose is not sufficiently analogous to dict.get or list.__getitem__.</div>
<div> </div></div><div><font face="arial, helvetica, sans-serif">--- Bruce</font><div><font face="arial, helvetica, sans-serif">Follow me: <a href="http://www.twitter.com/Vroo" target="_blank">http://www.twitter.com/Vroo</a> <a href="http://www.vroospeak.com/" target="_blank">http://www.vroospeak.com</a></font></div>
<br class="Apple-interchange-newline"></div>