subexpressions

Peter Otten __peter__ at web.de
Fri Jun 1 04:09:50 EDT 2007


Sergey Dorofeev wrote:

> Please help, is there way to use sub-expressions in lambda?
> For example, if I want to calculate sin(x^2)+cos(x^2) I must code:
> lambda x: sin(x*x)+cos(x*x)
> How to make x*x to be evaluated once?

>>> (lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +
cos(.5*.5)
True

The real answer is of course: Use a function.

Peter




More information about the Python-list mailing list