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

Nick Coghlan ncoghlan at gmail.com
Wed Sep 9 14:36:42 CEST 2009


Lie Ryan wrote:
> Greg Ewing wrote:
>> Stefan Behnel wrote:
>>
>>> It would therefore be nice to have a common ".any()" method on data
>>> structures that would just read an arbitrary item from a container.
>>
>> Rather than add a method to every container implementation,
>> it would be easier to provide a function:
>>
>>    def first(obj):
>>      return iter(ob).next()
>>
>> possibly with some embellishments to handle StopIteration,
>> allow for a default value, etc.
>>
> 
> Which would imply a request for last().

Not really - every iterator in Python is guaranteed to either have a
first value or throw an exception when you try to retrieve it via next().

There's no such guarantee that every iterator will terminate and hence
have a "last" value (cf. itertools.count).

Cheers,
Nick.

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



More information about the Python-ideas mailing list