[Python-ideas] get method for sets?

Paul Moore p.f.moore at gmail.com
Wed May 16 10:34:52 CEST 2012


On 16 May 2012 09:09, Mike Meyer <mwm at mired.org> wrote:
> Because next(iter(s)) makes the reader wonder "Why is this iterator
> being created?" It's a less expensive form of writing list(s)[0]. It's
> also sufficiently non-obvious that the closest I found on google for a
> discussion of the issue was the "for x in s: break" variant. Which
> makes me think that at the very least, this idiom ought to be
> mentioned in the documentation. Or if it's already there, then a
> pointer added to the set documentation.

I guess a doc patch adding a comment in the documentation of set.pop
that if you want an arbitrary element of a set *without* removing it,
then next(iter(s)) will give it to you, would be reasonable. Maybe you
could write one?

But I don't think it's particularly difficult to understand. It's very
Python-specific, sure, but it feels idiomatic to me.

Paul.



More information about the Python-ideas mailing list