identity = lambda x: x -- a Pythonic idiom?
George Demmy
gdemmy at layton-graphics.com
Sat Nov 17 09:53:22 EST 2001
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)
I did it so often, that stuff like this shows up:
identity = lambda x: x # like this in practice (lazy)
def identity(x): # Pythonic -- shows up in profiler, etc
return x
data = filter(identity, crufty_list)
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?
Curiously,
G
--
George Demmy
More information about the Python-list
mailing list