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

Terry Reedy tjreedy at udel.edu
Tue Oct 27 19:50:28 CET 2009

Raymond Hettinger wrote:
> A dict.get() can be meaningfully used in a loop (because the key can vary).
> A set.get() returns the same value over and over again (because there is 
> no key).

There are two ideas of set.get floating about:
1) get an arbitrary object
2) get the object in the set with the same 'value'(hash+eq) as an input 
arg (the intern case). In this case, there is a 'key', even if it is 
somewhat abstract rather that being an object.

Both could be done with the same method, depending on whether an arg is 
passed or not. The former is not useful in a loop; the latter could be.

If there is no match in case 2, the method could a) raise an exception, 
b) return None (which by its nature would never sensibly be looked up), 
or c) return an object specified by 'default=xxx' keyword arg.

Terry Jan Reedy

More information about the Python-Dev mailing list