[Python-ideas] get method for sets?
Mike Meyer
mwm at mired.org
Wed May 16 09:10:35 CEST 2012
On Wed, 16 May 2012 16:58:43 +1000
Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, May 16, 2012 at 02:32:15AM -0400, Mike Meyer wrote:
> > Is there some reason that there isn't a straightforward way to get an
> > element from a set without removing it? Everything I find either
> > requires multiple statements or converting the set to another data
> > type.
> > It seems that some kind of get method would be useful. The argument
> > that "getting an arbitrary element from a set isn't useful" is refuted
> > by 1) the existence of the pop method, which does just that,
> pop returns an arbitrary element, and removes it. That's a very
> different operation to "get this element from the set".
> The problem is, if there was a set.get(x) method, you have to pass x as
> argument, and it returns, what? x. So what's the point? You already have
> the return value before you call the function.
I guess I should have been explicit about what I'm was asking about.
I'm not asking for set.get(x) that returns "this element", I'm asking
for set.get() that returns an arbitrary element, like set.pop(), but
without removing it. It doesn't even need to be the same element that
set.pop() would return.
The name is probably a poor choice, but I'm not sure what else it
should be. pop_without_remove seems a bit verbose, and implies that it
might return the element a pop would.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
More information about the Python-ideas
mailing list