Re:Building a full-text search engine in 150 lines of Python...
by chaos_emergent on Friday January 24, 2025 @03:28AM
(#42811468)
Holy shit, I’ve been writing python for 15 years and it’s the first time I’ve seen None used as an identity function. That’s nuts! How does it work under the hood? Does filter have a special case for evaluating None?
Re:Building a full-text search engine in 150 lines of Python...
by echoangle on Friday January 24, 2025 @03:31AM
(#42811480)
Yes, that’s just a special case. You can’t call None as a function.
https://docs.python.org/3/library/functions.html#filter
> If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Re:Building a full-text search engine in 150 lines of Python...
by sroussey on Friday January 24, 2025 @05:55AM
(#42812016)
You can do the same thing in JavaScript with Boolean
Eg, arr.filter(Boolean)
Re:Building a full-text search engine in 150 lines of Python...
by evnp on Friday January 24, 2025 @07:00AM
(#42812297)
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`..
Re:Building a full-text search engine in 150 lines of Python...
by hacked_greenhat on Friday January 24, 2025 @10:36AM
(#42814107)
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.
Re:Building a full-text search engine in 150 lines of Python...
by evnp on Sunday January 26, 2025 @10:34AM
(#42830709)
Thanks 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.
Or you can do,
Not obvious, but giving None is like giving "lambda x : x" to filter().