Why does np.ndarray .__ deepcopy__ require an extra argument?

The duplicate will respond what, but not adequately, whyor why it is not mentioned in the documents, even if it is required. Read on ...


There are two ways to create a deep copy of the numpy array. One way is to use copy.deepcopyfrom a module copy. Another way is to call array.__deepcopy__directly.

From docs :

ndarray.__deepcopy__() → Deep copy of array.

Used if copy.deepcopycalled in an array.

The docs suggest that the correct way to call this function is without arguments. However....

In [47]: x
Out[47]: 
array([[1, 1],
       [2, 2]])

In [48]: x.__deepcopy__()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-00c3b8eda618> in <module>()
----> 1 x.__deepcopy__()

TypeError: __deepcopy__() takes exactly 1 argument (0 given)
Coming back

A TypeError! But...

In [51]: x.__deepcopy__(None)
Out[51]: 
array([[1, 1],
       [2, 2]])

And even...

In [52]: x.__deepcopy__([12345, 'blah blah'])
Out[52]: 
array([[1, 1],
       [2, 2]])

This function seems to require an argument, but this argument supposedly has nothing to do with the returned copy.

. ? , ?

+4
1

Python , __deepcopy__() memo (docs):

, __copy__() __deepcopy__(). ; . ; , . __deepcopy__() , deepcopy() , - .

-

"memo" ,

, .

, numpy , Python, .

+1

Source: https://habr.com/ru/post/1682863/


All Articles