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

Stephen J. Turnbull stephen at xemacs.org
Sat Oct 24 08:22:25 CEST 2009


Ben Finney writes:
 > Steven D'Aprano <steve at pearwood.info> writes:

 > > "get" is such a generic term that I don't believe that is a problem.
 > 
 > The problem above is made less problematic by the fact that the function
 > signature (e.g. 'foo_dict.get(key)') clarifies the answer to the
 > question "get what?". Whereas 'foo_set.get()' doesn't communicate much
 > at all to the reader.

I agree.

This is precisely why a couple of months ago people were proposing
names like ".getany()" for this API.

The problem brought up then was that pretty clearly people would then
do things like

    x = foo.getany()
    y = foo.getany()

expecting x and y to be different (ie, interpreting "any" as "random").
Pretty soon there was a whole family of proposals for such methods:
.getfirst(), .getlast(), .getrandom(), .getonly(), ....

I think it would be better to document the various ways of doing this,
and let each program define its own oneliner for the MySet.get() that
makes idiomatic sense in its use case.


More information about the Python-Dev mailing list