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

Georg Brandl g.brandl at gmx.net
Sat Sep 5 12:53:11 CEST 2009


Antoine Pitrou schrieb:
> 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))

or

any_item = compose(next, iter)

;)

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-ideas mailing list