[Python-ideas] Add an identity function

Devin Jeanpierre jeanpierreda at gmail.com
Sun Aug 7 04:18:49 CEST 2011

Nonetheless, Steven does have a point: the identity function is very
trivial to define in your own code (I do it lots), so there isn't much
benefit adding it.

The reason I would personally want it (and perhaps a few other helpful
"base case" functions, like constant(x)(y) = x) is to encourage its
use among people that haven't considered the concept. That isn't all
that good a reason, though. +0?


On Sat, Aug 6, 2011 at 10:07 PM, dag.odenhall at gmail.com
<dag.odenhall at gmail.com> wrote:
>> So, while I want an identity function, I don't want an identity function
>> which requires actually calling a function at runtime. What I really want is
>> compiler black magic, so that I can write:
>> def len_sum(iterable, transformation=None):
>>    """Sum iterable, returning length and sum in one pass."""
>>    count = 0
>>    total = 0
>>    for x in iterable:
>>        count += 1
>>        total += transformation(x)
>>    return count, total
>> and the compiler is smart enough to do the Right Thing for me, without
>> either the need to repeat code, or the function call overhead. (And also a
>> pony.) Without that level of black magic, I don't think adding an identity
>> function to the standard library is worth the time or effort.
> -1 on silently pretending that None is callable as the identity
> function. If you have an actual function, an optimizer could probably
> strip it away in cases like len_sum.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list