parse-time optimizations

Joshua Marshall jmarshal at mathworks.com
Thu May 31 14:15:41 EDT 2001


Carlos Ribeiro <cribeiro at mail.inet.com.br> wrote:
>> >     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.

I didn't realize this was possible in Python.  Can you give an example
of how a new, right-associative addition operator could be defined?

> 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.

I agree.



More information about the Python-list mailing list