[Numpy-discussion] Funny business with 'is' operator?
Chris Barker - NOAA Federal
chris.barker at noaa.gov
Fri Sep 6 12:30:34 EDT 2013
On Fri, Sep 6, 2013 at 9:19 AM, James Bergstra <bergstrj at iro.umontreal.ca>wrote:
> def test_is(self):
> a = np.empty(1)
> b = np.empty(1)
> if a.data is not b.data:
> assert id(a.data) != id(b.data) # <-- fail
>
>
I'm not familiar with the internals, but:
In [27]: a = np.empty(1)
In [28]: a.data
Out[28]: <read-write buffer for 0x122df30, size 8, offset 0 at 0x3501860>
so .data is a buffer object, wrapped around a particular pointer.
In [29]: id(a.data)
Out[29]: 55581376
In [30]: id(a.data)
Out[30]: 55581728
but it looks like the buffer object itself is created on the fly -- so you
are getting a different pyton object, even though it wraps the same data
pointer. So you need to compare the value of the pointer compared, not the
identity of the buffer object.
Not sure how to get that value though, other that parsing __repr__
In [44]: a.data.__repr__().split()[-1][:-1]
Out[44]: '0x3512100'
ugly hack -- there must be a better way!
But:
In [38]: a = np.zeros((100,))
In [39]: b = a[50:]
In [40]: a.data
Out[40]: <read-write buffer for 0x1443990, size 800, offset 0 at 0x3501da0>
In [41]: b.data
Out[41]: <read-write buffer for 0x1617130, size 400, offset 0 at 0x3501c20>
a and b share memory, but don't have the same data pointer as b's is offset.
HTH,
-Chris
> I'm trying to write an alternate may_share_memory function.
>
> Thanks,
> - James
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130906/e4524503/attachment.html>
More information about the NumPy-Discussion
mailing list