Need an identity operator because lambda is too slow
David Wahler
dwahler at gmail.com
Sun Feb 18 07:10:03 EST 2007
On Feb 18, 5:59 am, "Deron Meranda" <deron.mera... at gmail.com> wrote:
> Consider a much-simplified example of such an iteration where
> sometimes you need to transform an item by some function, but most of
> the time you don't:
>
> if some_rare_condition:
> func = some_transform_function
> else:
> func = lambda x: x
> for item in some_sequence:
> item2 = func(item)
> ..... # more stuff
>
> Now the "lambda x:x" acts suitably like an identity operator. But it
> is very slow, when compared to using more complex-looking code:
>
> do_transform = some_rare_condition
> for item in some_sequence:
> if do_transform:
> item2 = transform_function(item)
> else:
> item2 = item
> ..... # more stuff
>
> What python needs is something like a built-in "operator.identity"
> function, which acts like "lambda x:x", but which the byte compiler
> could recognize and completely optimize away so there is no function
> call overhead.
Unless I'm misunderstanding you, you seem to be proposing that the
compiler should avoid generating the call to func2() if it has a
certain value. How could that information possibly be available at
compile time?
-- David
More information about the Python-list
mailing list