So what's happening here?
Gary Herron
gary.herron at islandtraining.com
Fri Jun 5 09:57:59 EDT 2015
On 06/05/2015 06:39 AM, Todd wrote:
> On Fri, Jun 5, 2015 at 3:23 PM, Gary Herron
> <gary.herron at islandtraining.com
> <mailto:gary.herron at islandtraining.com>> wrote:
>
> On 06/05/2015 06:11 AM, Paul Appleby wrote:
>
> On Fri, 05 Jun 2015 14:55:11 +0200, Todd wrote:
>
> Numpy arrays are not lists, they are numpy arrays. They
> are two
> different data types with different behaviors. In lists,
> slicing is a
> copy. In numpy arrays, it is a view (a data structure
> representing some
> part of another data structure). You need to explicitly
> copy the numpy
> array using the "copy" method to get a copy rather than a
> view:
>
> OK, thanks. I see.
>
> (I'd have thought that id(a[1]) and id(b[1]) would be the same
> if they
> were the same element via different "views", but the id's seem
> to change
> according to rules that I can't fathom.)
>
> Nope. It's odder than that. a[1] is still a view into the
> inderlying numpy array, and your id is the id of that view. Each
> such index produces a new such view object. Check this out:
>
> >>> import numpy
> >>> a = numpy.array([1,2,3])
> >>> id(a[1])
> 28392768
> >>> id(a[1])
> 28409872
>
> This produces two different view of the same underlying object.
>
>
> a[1] and b[1] are not views:
>
> >>> a[1].flags['OWNDATA']
> True
> >>> b[1].flags['OWNDATA']
> True
> >>> a[1:2].flags['OWNDATA']
> False
Right. My bad. Each execution of a[1] creates a new numpy.int64 object
with the value from the array.
>>> type(a[1])
<class 'numpy.int64'>
Each execution of id(a[1]) creates an int64 object which is immediately
used and then deleted. Two successive executions of id(a[1]) may or may
not reuse the same piece of memory, depending on what else is going on
in memory. Indeed when I produced the above example with id(a[1]), a
third and fourth runs of id(a[1]) did indeed repeat 28409872, but they
are all new creations of an int64 object which happen to use the same
recently freed bit of memory.
Gary Herron
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150605/d38d1505/attachment.html>
More information about the Python-list
mailing list