question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding `.copy()` method

See original GitHub issue

Some array libraries implement a .copy() method (like NumPy). While there are some indirect ways to get at this now (asarray, reshape, etc.), currently the API lacks a way to do this directly and without other potential side-effects. Should add a method (unlike a function) would ensure the new array has the original array’s type simply. Curious if there is appetite for including this in the API.

Related is a question of what interplay there is with __copy__ and/or __deepcopy__ (if any).

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jakirkhamcommented, Oct 20, 2022

To summarize the ask, library functions handling general arrays may want to copy as they want an array they can mutate safely without affecting user provided data.

We concluded that we want a function (not a method) and one needs to do a namespace lookup (x.__array_namespace__) to figure it out (as the type is likely not known by the library).

There is a separate question of what we call it. Either a new function (copy, clone) or use an existing one (asarray(x, copy=True)). We would also want to spell out that some libraries (Dask, JAX, maybe others) may not actually copy the underlying data.

__copy__ and __deepcopy__ are different enough (Dask would copy graphs, JAX something similar, etc.) that it is worth specifying this path may not copy the array data (if that is the user’s concern) and that using the function above (name to be decided) would be preferred for data copying.

0reactions
rgommerscommented, Nov 4, 2022

@asmeurer good point. Maybe something like “must ensure that the returned array does not share data with another array, either by copying the data to a new memory location or in some other way (e.g., this property is guaranteed by design)”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Best practice to implement copy method in a class?
Follow implementation makes just copy, without calling the init() method. class MyClass(BaseClass): def __init__(self, name): super().
Read more >
Python Set copy() (with Examples) - Programiz
The copy() method returns a copy of the set. In this tutorial, you will learn about the Python Set copy() method with the...
Read more >
Python Set copy() Method with examples - BeginnersBook
The copy() method in Python returns a copy of the Set. We can copy a set to another set using the = operator,...
Read more >
Python list copy() method - GeeksforGeeks
Python List Copy() methods return a shallow copy of a list(returns a new list without modifying the original lists).
Read more >
copy — Shallow and deep copy operations — Python 3.11.1 ...
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found