best cumulative sum

Peter Otten __peter__ at web.de
Thu Nov 24 03:45:43 EST 2005


Alan aka David Isaac wrote:

> "Peter Otten" <__peter__ at web.de> wrote in message
> news:dm2b8b$p8n$02$1 at news.t-online.com...
>> You are in for a surprise here:
> 
> You got that right!
> 
>> >>> def empty():
>> ...     for item in []:
>> ...             yield item
>> ...
>> >>> bool(empty())
>> True
> 
> Ouch.
> 
>> >>> bool(iter([]))
>> True # python 2.3 and probably 2.5
>>
>> >>> bool(iter([]))
>> False # python 2.4
> 
> Double ouch.
> I was relying on Python 2.4 behavior.
> What is the reasoning behind the changes?
> (Can you offer a URL to a discussion?)

Raymond Hettinger introduced an optimization that looked cool until it broke
some of Guido's code, see

http://mail.python.org/pipermail/python-dev/2005-September/056594.html

> So, is the only way to test for an empty iterable
> to see if it can generate an item?  I found this:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413614
> Seems like a reason to rely on sequences ...

I'd rather have a second look whether the test is really needed.

Peter




More information about the Python-list mailing list