Message380613
> I assume you've been recommending this?
Not really, but it does come up and I've seen it in customer code more than once.
I do show people this:
>>> data = [10.5, 3.27, float('Nan'), 56.1]
>>> list(filter(isfinite, data))
[10.5, 3.27, 56.1]
>>> list(filterfalse(isnan, data))
[10.5, 3.27, 56.1]
The question does arise about how to do this for None using functional programming. The answer is a bit awkward:
>>> from operator import is_not
>>> from functools import partial
>>> data = [10.5, 3.27, None, 56.1]
>>> list(filter(partial(is_not, None), data))
[10.5, 3.27, 56.1]
From a teaching point of view, the important part is to show that this code does not do what people typically expect:
>>> data = [10.5, 0.0, float('NaN'), 3.27, None, 56.1]
>>> list(filter(None, data))
[10.5, nan, 3.27, 56.1]
FWIW, this issue isn't important to me. Just wanted to note that one of the idioms no longer works. There are of course other ways to do it. |
|
Date |
User |
Action |
Args |
2020-11-09 20:04:23 | rhettinger | set | recipients:
+ rhettinger, gvanrossum, terry.reedy, steven.daprano, ethan.furman, serhiy.storchaka, josh.r, veky, xtreak |
2020-11-09 20:04:23 | rhettinger | set | messageid: <1604952263.13.0.6948004846.issue35712@roundup.psfhosted.org> |
2020-11-09 20:04:23 | rhettinger | link | issue35712 messages |
2020-11-09 20:04:23 | rhettinger | create | |
|