[Python-ideas] data structures should have an .any() method

Nick Coghlan ncoghlan at gmail.com
Fri Sep 4 14:17:09 CEST 2009

Matteo Dell'Amico wrote:
>> The shorthand expression above also suffers from the obscurity that
>> Stefan was complaining about - there is very little to hint that
>> "next(iter(obj))" means "get an arbitrary object out of a container".
>> The StopIteration exception this approach will throw for an empty
>> container is also rather unhelpful.
> Why? next(iter(obj)) means, pretty explicitly to me, "iterate on obj and
> give me one element".

Because it overspecifies the semantics of what you're trying to do. It
just happens that when the requirement is "get me any object in this
container" the design of Python means that the easiest implementation is
"get me the first object in this container".

The expression form then reflects the implementation rather than the
algorithmic intent.

That said, this concise way of implementing the desired feature is
certainly one of the reasons I am -0 on the idea of adding it to the
standard library.


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

More information about the Python-ideas mailing list