[Numpy-discussion] Funny business with 'is' operator?:
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.
a.data is a.data
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.
> - James
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion