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

Ben Finney ben+python at benfinney.id.au
Sat Oct 24 05:02:48 CEST 2009

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

> On Sat, 24 Oct 2009 10:26:27 am Ben Finney wrote:
> > Steven D'Aprano <steve at pearwood.info> writes:
> > > The lack of get() in sets and frozensets is sounding more and more
> > > to me like the victory of purity over practicality.
> >
> > What would be the input to ‘set.get’?
> It wouldn't take any input.

That is even less obvious. I would expect a parameter-less ‘set.get’ to
get the set. Not terribly useful, but the name and function signature
doesn't suggest anything else.

> "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.

If we want a method that gets one item from a set, perhaps the name can
make it clearer: name it ‘set.getitem’. But which item should it get?
The ‘__getitem__’ special method of lists and dictionaries requires an
index or key as parameter.

 \             “Roll dice!” “Why?” “Shut up! I don't need your fucking |
  `\     *input*, I need you to roll dice!” —Luke Crane, demonstrating |
_o__)                       his refined approach to play testing, 2009 |
Ben Finney

More information about the Python-Dev mailing list