subexpressions

Steve Holden steve at holdenweb.com
Fri Jun 1 13:09:05 CEST 2007

```Sergey Dorofeev wrote:
> "Peter Otten" <__peter__ at web.de> wrote in message
> news:f3ok60\$vp7\$03\$1 at news.t-online.com...
>> Sergey Dorofeev wrote:
>>
>>> 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.
>
> But what about something like
>
> lambda x: sin(y)+cos(y) where y=x*x
>
> ?
> May be this could be a PEP? If there is no straight way to do this.
>
>
Or maybe it could be made a part of some other language. When
straightforward mechanisms (in rhis case, function definitins) exist to
avoid repeated computations it's very unlikely that such mangled
constructions will be made a part of Python.

If it *were* considered, you should at least change the "where" to
"for", and extend it to unpacking assignment to allow

lambda x, y: (sin(xx+yy) + cos(xx+yy) for xx, yy = x*x, y*y

regards
Steve
--
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com        squidoo.com/pythonology
tagged items:         del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

```