[Python-ideas] get method for sets?

Mike Meyer mwm at mired.org
Wed May 16 09:52:01 CEST 2012


On Wed, 16 May 2012 17:26:45 +1000
Steven D'Aprano <steve at pearwood.info> wrote:

> On Wed, May 16, 2012 at 03:10:35AM -0400, Mike Meyer wrote:
> > 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.
> 
> Could this helper function not do the job?
> 
> def get(s):
>     x = s.pop()
>     s.add(x)
>     return x

Sure, if you don't mind munging the set unnecessarily. That's more
readable, but slower and longer than:

def get(s):
    for x in is:
        return s

> Of course, this does not guarantee that repeated calls to get() won't 
> return the same result over and over again. If that's unacceptable, 
> you'll need to specify what behaviour is acceptable -- i.e. what your 
> functional requirements are. E.g.
> "I need the element to be selected at random."
> "I don't need randomness, returning the elements in some arbitrary 
> but deterministic order will do, with no repeats or cycles."
> "I don't care whether or not there are repeats, so long as the same 
> element is not returned twice in a row."
> "Once I've seen every element, I expect get() to raise an exception."
> etc.

My requirements are "I need an element from the set". The behavior of
repeated calls is immaterial.

> And I guarantee that whatever your requirements are, other people will 
> want something different.

That's not what I found in my google results. They were all pretty
much asking for what I was asking for, and didn't care what happened
beyond the first call.

I believe you're assuming that the purpose of this method is to start
an iteration through the set. That's not the case at all, and a single
call to pop would be perfectly acceptable, except I would then need to
put the element back.

If that were the purpose, I'd agree with you - between iteration and
pop, we've covered most of the ways you might want to iterate through
the elements. But the point isn't to iterate through the elements,
it's to examine a single element.

> Once you have your requirements, you can start thinking about 
> implementation (e.g. how does the set remember which elements have 
> already been get'ed?).

Doesn't need to, because it doesn't matter.

> > 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.
> Are you suggesting that get() and pop() should not return the same 
> element?

I'm suggesting there is no requirement that they return the same
element.

	<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