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

Ben Finney ben+python at benfinney.id.au
Sat Oct 24 06:06:28 CEST 2009

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

> On Sat, 24 Oct 2009 02:02:48 pm Ben Finney wrote:
> > 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.
> You already have the set. Why would you need a method that you call
> that returns the set you already have?

Exactly, hence the confusion. I think the method name ‘set.get’ is poor
for that reason.

> A method called "get" obviously gets something, and if it takes no
> arguments the only thing is has access to is the set. The obvious
> things it could get are the set as a whole or some part of the set.

Which then raises the question “what part of the set does it get?”,
which the function signature does nothing to answer. I'm proposing that
a no-parameters ‘set.get’ is needlessly confusing to think about.

> Since there's no obvious reason to choose one subset over another
> subset, the only useful thing it could return is a single arbitrary
> item.

That's not at all obvious, IMO. The name doesn't give much help at all
in getting to that conclusion, and isn't easily associated with that

> You are demanding a level of intuitiveness that few, if any, functions
> in the standard library would be able to meet.

I'm not demanding intuitiveness; all programming interfaces are learned.
I'm asking for ease of learning and later recall.

> That's not a problem with the built-in function. I took a guess about
> the API, and guessed wrong. I'll learn from this, and get it right
> next time.

Which is partly my point. Such a narrow use case (“get an arbitrary item
from the set, without specifying anything about what that item might
be”) is a mismatch for such a generic name. It makes it unnecessarily
difficult to make the association.

Since the use case is so specific, I would expect the name to be
specific too, to better match the use case.

(Since I know how your mind works, I can anticipate facetious
fifty-character-long names bubbling up already in a response from you.
Let's just assume that joke is already done :-)

 \                             “Holy tintinnabulation, Batman!” —Robin |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

More information about the Python-Dev mailing list