[Python-Dev] Retrieve an arbitrary element from a set withoutremoving it

Raymond Hettinger python at rcn.com
Mon Oct 26 20:23:31 CET 2009

Chris Bergstresser]
>   I like the proposed set.get() method, personally. 

Sets have been implemented in many languages, but  I've only 
seen one that implemented this functionality  (the "arb" function 
in SETL).  For the most part, it seems that this is an uncommon need.

Also consider that there is value to keeping the set-api as
simple as possible.  Right now, anyone who was exposed
to the basics of sets in school can understand the set-api
with a near zero learning curve.  Some of that would be
lost if you add methods that make identity/equality distinctions
or that fetch the same arbitrary value over and over again.

Besides, it is trivial to write a short function that encapsulates
this behavior if it is part of your personal style of expression.

>   The existing methods aren't great for accomplishing this, mainly
> because they're obfuscatory.  "iter(s).next()" is probably clearest,
> and at least will throw a StopIteration exception if the set is empty.
> "for x in s: break" is just confusing ...

A short comment would do wonders.


P.S.  It is weird that this thread is gaining traction at the same
time as the moratorium thread.  Go figure :-)

More information about the Python-Dev mailing list