On Sep 5, 2007, at 7:37 PM, Robert Bradshaw wrote:

>> But I'm a bit concerned.... why doesn't Python do this itself? I just
>> had a look at the implementations of some arithmetic operations on
>> e.g. floats and strings in the python source, and they never use this
>> trick.
>>
>> I would feel much more comfortable if there was some "mainstream"
>> precedent for this.
>
> Good questions. I think there is a good reason why Python doesn't do
> this. First, there is no requirement in Python that a.__add__(b) ==
> a.__iadd__(b), I think it is safe to consider this a *bug* for sage
> Elements. Secondly, it is possible to write a python extension
> classes that will break this. Specifically, imagine this code:
>
>      PyObject* tmp = <PyObject *>x
>      x += y # now tmp has changed as well, because we think we own
> the only reference to x
>      z = <object>tmp # which has been mutated
>
> This is not typical SAGE Cython code, and would only apply in that
> context (x and y would have to be elements). I can't even come up
> with an example that breaks x+y (by mutating x) but one could perhaps
> do pointer manipulation and call PyNumber_Add directly (after re-
> declaring it to take PyObject*) or do something directly in C.

Hmmmmm this has me a bit worried.

The corollary of what you just said is: if someone writes some code  
that tries to optimise by avoiding a refcount that they think is safe  
to avoid, then it can break your inplace operators.

Like for example, suppose you are writing a cython function that  
operates on a python list containing Elements. You know that the  
Elements are already refcounted (from the list), so when you do  
arithmetic on them, you can avoid refcounting. For example maybe  
you're doing a row operation in a generic matrix class, or something  
like that.

david


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to