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

Antoine Pitrou solipsis at pitrou.net
Sat Sep 5 04:45:13 CEST 2009

Nick Coghlan <ncoghlan at ...> writes:
> 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".

I don't agree.
Since iteration is such a frequent operation, any container which doesn't
provide cheap iteration could be considered badly designed and/or badly
implemented. Therefore it makes sense to rely on iteration when implementing
other primitives.

People worrying that it expresses implementation rather than intent can write
the trivial abstraction by themselves:

def any_item(x):
    return next(iter(x))



More information about the Python-ideas mailing list