6 ms·
Is this any different from `filter(bool, tokens)`? (similar to the JS equivalent mentioned in a sibling) If not, I'm now curious why the `None` special case wa
by evnp 2y ago
Is this any different from `filter(bool, tokens)`? (similar to the JS equivalent mentioned in a sibling)
If not, I'm now curious why the `None` special case was added to `filter`..
- hacked_greenhat 2y agohttps://stackoverflow.com/questions/8748036/is-there-a-builtin-identity-function-in-python https://stackoverflow.com/questions/8748036/is-there-a-built... While this doesn't explain why filter accepts None as the lambda, I think the fact that there are no builtin identity function kind of explains why. It's easier to write None rather than put an identity lambda. I reckon it also makes it easier for a compiler to optimize this specific case.
- evnp 2y agoThanks for the explanation. I take your point, but in the `filter` case `bool` seems just as easy as `None`, but with the added benefit that no awareness of a special case is needed -- any python programmer should understand how the `bool` builtin works. Curious if there's any downside.