[Python-ideas] get method for sets?

Nick Coghlan ncoghlan at gmail.com
Wed May 16 10:08:11 CEST 2012


On Wed, May 16, 2012 at 5:40 PM, Mike Meyer <mwm at mired.org> wrote:
> >From poking around, at least at one time the fastest implementation
> was the very confusing:
>
> def get_from_set(s):
>    for x in s:
>        return x

Why is this confusing? The operation you want to perform is "give me
an object from this set, I don't care which one".

That's not an operation that applies just to sets, you can do it with
an iterable, therefore the spelling is one that works with any
iterable: next(iter(s))

This entire thread is like asking for s.length() when len(s) already works.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list