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" <bergs...@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@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to