[Numpy-discussion] Funny business with 'is' operator?:
Nathaniel Smith
njs at pobox.com
Fri Sep 6 12:28:42 EDT 2013
The .data attribute is generated on the fly when accessed. So it returns an
anonymous temporary that's deallocated as soon as it's no longer needed.
a.data is b.data needs both objects, so both get allocated and then
compared. In the second one though, each object gets allocated one at a
time and then immediately released. So the a.data and b.data objects are
different... but since they have non-overlapping lifetimes, they happen to
be out at the same memory location. id() is only guaranteed to be unique
while the object is alive.
Notice also:
a.data is a.data
-> False
-n
On 6 Sep 2013 17:21, "James Bergstra" <bergstrj at iro.umontreal.ca> wrote:
> Hi, could someone help me understand why this assertion fails?
>
> 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 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130906/9707d77b/attachment.html>
More information about the NumPy-Discussion
mailing list