subexpressions

Diez B. Roggisch deets at nospam.web.de
Fri Jun 1 08:08:36 EDT 2007


Steve Howell wrote:

> --- Sergey Dorofeev <sergey at fidoman.ru> wrote:
>> > What syntax would you suggest for a lambda
>> enhanced to cover your use
>> > case?
>> > I suppose you will end up with roughly the same
>> number of characters, all
>> > crammed in one line -- or broken into lines at a
>> random position as it
>> > happens with overambitious list comprehensions.
>> 
>> Agree, this argument is strong.
>> 
> 
> FWIW there's the possibility that even without a
> subexpression syntax, some Python implementations
> would detect the duplication of x*x and optimize that
> for you.  It would have to know that x*x had no side
> effects, which I think is a safe assumption even in a
> dynamic language like Python.

Of course it's not. You can create arbitrarily overloaded __mul__-operators
here. Unless you can guarantee that the objects in question are immutable
(which you can only for python-builtins), you can't be sure that there are
no sideeffects. And the needed guarding statements for this kind of
optimization might well eat up the gain from not multiplying two numbers
together.

Diez




More information about the Python-list mailing list