[Python-ideas] Add an identity function

dag.odenhall at gmail.com dag.odenhall at gmail.com
Sun Aug 7 04:07:26 CEST 2011

> 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.

More information about the Python-ideas mailing list