Sure.  I didn’t originally, because I thought it would require an entire custom 
array container, but the following trivial example actually shows the behavior 
in question:

import numpy

class MyThing(object):
    def __array__(self, dtype=None, copy=None):
        print(f"MyThing.__array__(dtype={dtype}, copy={copy})")
        return numpy.ones((5, 5))

u = numpy.zeros((5, 5))
v = MyThing()

u[...] = v

If you run this code, as part of the final assignment statement, the __array__ 
method is called for ‘v’ with copy=True.  Why?

—
Daniel Israel
XCP-4: Continuum Models and Numerical Algorithms
d...@lanl.gov

On Dec 25, 2024, at 3:23 PM, Steven Ellis <stevenalonzoel...@gmail.com> wrote:

Hi David,

New to the listserv, but, maybe you can provide a reproducible example?

Steven

On Wed, Dec 25, 2024, 2:19 PM Israel, Daniel M via NumPy-Discussion 
<numpy-discussion@python.org<mailto:numpy-discussion@python.org>> wrote:
I was updating some code that uses a custom array container built with the 
mixin library.  Specifically, I was trying to eliminate some warnings due to 
the change to the __array__ interface to add a copy argument.  In doing so, I 
discovered that, for two objects u, v in my container class, the code:

u[…] = v

performs a copy on v.  Specifically, it calls __array__() with copy=True.  This 
seems unnecessary and wasteful of memory.  Can someone explain to me what is 
happening here?

Thanks.

—
Daniel Israel
XCP-4: Continuum Models and Numerical Algorithms
d...@lanl.gov<mailto:d...@lanl.gov>

_______________________________________________
NumPy-Discussion mailing list -- 
numpy-discussion@python.org<mailto:numpy-discussion@python.org>
To unsubscribe send an email to 
numpy-discussion-le...@python.org<mailto:numpy-discussion-le...@python.org>
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/<https://urldefense.com/v3/__https://mail.python.org/mailman3/lists/numpy-discussion.python.org/__;!!Bt8fGhp8LhKGRg!A_N5Cns4800odNjjStN3QAH94TwUNBEkvr7lqW4-PnnUiGS_kckVyXKfS4QTR4YADAXXGOTbKZ-6RDYTC7knRCMj$>
Member address: stevenalonzoel...@gmail.com<mailto:stevenalonzoel...@gmail.com>

_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to