parse-time optimizations

Carlos Ribeiro cribeiro at mail.inet.com.br
Thu May 31 15:11:30 CEST 2001


At 13:54 30/05/01 +0000, jcm wrote:
>Delaney, Timothy <tdelaney at avaya.com> wrote:
> > Very true. Constant folding could only be used in unambiguous 
> situations ...
> > and most such things aren't unambiguous :(
>
> > Indeed
>
> >     x + 1 + 2 + 3
>
> > couldn't even be folded down to
>
> >     x + 1 + 5
>
>This is an unambiguous situation.

No, it is not unambiguous. It assumes that the + operator is associative - 
which is true for the case of the "normal" addition. However, due to 
Python's dynamic nature, x can be any kind of object. It can define a new 
addition operation without the nice properties we all take for granted.

Before you tell me that this can't happen, please note that there are some 
mathemathical constructs that are not transitive, reflexive and/or 
associative. In group theory you can find such constructs; matrix 
multiplication also have this property.

So x+1+2+3 is not the same as x+1+5: it all depends on the definition of 
the add operator, as supplied by the x object.

Now, IF Python spec is changed to state that the add operator MUST BE 
associative...


Carlos Ribeiro






More information about the Python-list mailing list