list slice and generators

Peter Otten __peter__ at web.de
Wed Nov 25 13:22:39 EST 2015


Ian Kelly wrote:

>>assert metrics
> 
> metrics is always going to be an itertools.chain object at this
> assert, so how could the assertion ever fail?

Should an assertion ever fail? 

>From your reaction I conclude that it was puzzling and a comment like

# always true in a boolean context

would have been the better choice. 


The problem I was half-heartedly addressing is that

>>>      if not metrics:
>>>         return None

"works" for empty lists but not for "empty" iterators. If it were my code 
I'd probably allow the average calculation to raise an exception and the sum 
to return 0, but there's always a limit to changing other people's code.





More information about the Python-list mailing list