[Tutor] map, filter and lambda functions [filter can take None]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 23 Aug 2001 00:30:16 -0700 (PDT)

> > Now I'm curious as to *why* it works. 'None' doesn't strike me as a
> > function that returns a value. Although, it says in the documentation
> > somewhere, that None on map works as the identity function. This
> > didn't bother me, though. It seemed like "No map...gives you same
> > thing you started with."
> *Why* it works? Just because filter is designed to work that way. 
> Initially I was going to suggest a way of doing that, but instead of 
> None, I was going to suggest the truth function from the operator 
> module.
> None certainly isn't a function though.

[Note: skip if you're not interested in looking at Python icky C

Feeding 'None' as a "function" is a special case that's particular only to
Python's implementation of filter(), that is, this is hardcoded to
recognize this unusual input.  It certainly doesn't do it for a
human-intuitive reason.  *grin*

If we're interested in looking at C code, we can glance at the relevant
code in the 'bltinmodule.c' file under the function "builtin_filter()":

## Somewhere in Python/bltinmodule.c

/* Warning: this is adulterated code: I've chopped off the reference
   counting and error trapping code from this block.  You may want to look
   at the real thing at your leisure.  In Python 2.1, this is around
   line 214 of the file "bltinmodule.c".
		if (func == Py_None) {
			good = item;
		else {
			good = PyEval_CallObject(func, arg);
		ok = PyObject_IsTrue(good);
		if (ok) { ...                     /* let's stop here */

I dunno if it's a blemish or not that filter() behaves this way.  My
understanding is that the designers felt that testing an element for
trueness was such a common case that it deserved to be optimized, even at
the cost of some obviousness.  Personally, I've always opted for:

    filter(lambda x: x, some_list_of_things)

rather than

    filter(None, some_list_of_things)

but taking advantage of the None shortcut does make it more efficient for
the machine.