identity = lambda x: x -- a Pythonic idiom?

George Demmy gdemmy at layton-graphics.com
Sat Nov 17 16:15:22 EST 2001


Erik Max Francis <max at alcyone.com> writes:

> George Demmy wrote:
> 
> > I do a bunch of list-oriented programming with a bunch of one-off
> > programs. For a variety of reasons these lists tend to have a bit
> > of cruft associated with them (I don't write it -- just process
> > it!). So stuff like this shows up quite a bit:
> > 
> > data = filter(lambda x: x, crufty_list)
> 
> This doesn't even make sense to me.  filter applies the function for
> each element in the sequence, and returns a new list of only those
> elements for which the function returned true.  Why is using the
> identity function for this purpose useful?
>
> If you really want to select the true sequence, then from a mathematical
> standpoint what you want is operator.truth, not the identity function. 
> Obviously they do the same thing in this context, but they have
> different meanings.  (Or a None passed as the function would as well.)

My understanding of Pythonic truth anything other than None, (), [],
{}, '', and 0 is true. I don't know what mathematical truth is -- got
a approachable reference for an interested non-mathematician? If
Pythonic truth were to be redefined, e.g., to true and false objects,
I'd conceed the point. As a matter of fact, that would make more sense
to me -- I find and object with a value of 0 to be false a little
disorienting... 

I guess it's guessability that's partially at issue. For map and
filter None implies the identity function as part of the library
specification. It's not true in other contexts where it *might* be
true. E.g,

apply(None, "my string") 

raises an exception whereas

map(None, "my string") -> ['m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g']
filter(None, "my string") -> 'my string'

That they do might be confusing if you didn't know the specific
implication of passing None to these functions were. Conversely, if
you buy into the do-the-map-thing using None, then you might be
surprised that apply-nothing-to x blows up.

> > There are, of course, other nifty things to do with identity, and I
> > find it used all the time -- far more often than many Python
> > built-ins. How prevalent is identity usage in the Python community? Is
> > there anyone that would rather suffer hellfire and damnation that do
> > something like this?
> 
> There's nothing wrong with using an identity function in context (though
> your claim that you use it more often than many builtins is strange,
> since it explicitly doesn't do anything).  I'm just curious what you're
> doing such that the identity function pops up very frequently.  It could
> be there's a better way to get what you're going after.

Not *so* strange, really. I work with some libraries that take
multiple functions as arguments, and occasionally you don't want some
of them to do anything, or you want whatever is passed to be passed on
down the line. This crops up in some functions that iterate over
dictionaries, does things with the keys and values, and returns a new
dictionary with the results.

All that being said, the frequency of identity *misuse* will diminish,
since I was politely reminded that

filter-or-map(None, list)

is shorthand for 

filter-or-map(lambda x: x, sequence)

I will almost certainly use the identity function explictly or
implicitly more often than xrange, ord, unicode, et al., useful though
these functions be.

Cheers,

G


-- 
George Demmy




More information about the Python-list mailing list