On 9/27/07, Fernando Perez wrote:
> ...
> In [12]: a = N.arange(10)
>
> In [13]: sys.getrefcount(a)
> Out[13]: 2
>
> Here the refcount is 2, as expected: the original array and the name
> 'a' pointing to it.
>
> Now, a naked, unassigned slice of the original array has a refcount of 1:
>
> In [14]: sys.getrefcount(a[::2])
> Out[14]: 1
>

Whoa! This looks wrong to me. The slice of some larger object (which
in turn has a reference to it) should not have a reference count of 1.
(-: Ok, maybe it should be 1.00001 but not 1! ;-) The mutable larger
object in which it is contained certainly *references* parts of
itself. No?

> And yet, modifying it in-place has an effect on the original object
> (which we have called 'a'):
>
> In [15]: a[::2].fill(999)
>
> In [16]: a
> Out[16]: array([999,   1, 999,   3, 999,   5, 999,   7, 999,   9])
>

Uggh! Awful. :-(

>
> The slice 'a[::2]' has a refcount of 1, so it appears 'safe' from a
> purely refcounting argument.  But it actually shares memory with the
> original N.arange(10) object, so it can overwrite it.
>

As far as I am concerned "sharing memory" is equivalent to a reference.

> Note that I'm only pointing this out as a word of caution that there
> are cases where refcounting arguments can be a bit delicate.  The
> basic idea of reusing in-place objects as an optimization is a very
> good one for cases where you know that your underlying objects simply
> don't expose any interface for antics like the above.
>

I think your points are well taken. There certainly seems to be some
confusion and a lot written about this subject on the web. For example
from the Python docs:

http://docs.python.org/ext/refcountsInPython.html

-----
Nobody ``owns'' an object; however, you can own a reference to an
object. An object's reference count is now defined as the number of
owned references to it. The owner of a reference is responsible for
calling Py_DECREF() when the reference is no longer needed. Ownership
of a reference can be transferred.
-------

And it defines the concept of a "borrowed" reference, etc., etc.

Regards,
Bill Page.

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