3 ms·
It's even worse; the book is wrong. That implementation of contains_zero is not idiomatic to anyone who writes Python.
by empthought 9y ago
It's even worse; the book is wrong. That implementation of contains_zero is not idiomatic to anyone who writes Python.
- hxtk 9y agoNot only is it not idiomatic; it's functionally incorrect. An iterable containing `[]`, `None`, or any other value that is false in a boolean context would return true. They've assumed that `(0 -> false) -> (false -> 0)`, which is simply not true. Even if you're dealing with a context that cannot pass in an iterable with non-zero "falsy" values then it needs to be explicitly documented. At that point, why not "document" it by being explicit with the code. This is how I'd do it, but I feel like it could be made cleaner: def contains_zero(iterable): return any([i is 0 for i in iterable]) Edit: I forgot how to python. It's `return 0 in iterable`.
- deleted 9y ago[deleted]