A similar question a week ago: How to understand the ndarray.reshape function?
np.reshape(a, newshape)converted as a.reshape(newshape). But a.reshape- a built-in, compiled method. So the details of how it handles newshapeare hidden (for Python programmers).
, newshape . . .
, , . a[:,1,3] a.__getitem__((slice(None),1,3)). a[(:,1,3)] , ind = (slice(None),1,3); a[ind].
, () :
In [58]: def foo(*args):
...: if len(args)==1:
...: args = args[0]
...: print(args)
...:
In [59]: foo(1,2,3)
(1, 2, 3)
In [60]: foo((1,2,3))
(1, 2, 3)
, :
In [61]: foo(1)
1
In [62]: foo((1,))
(1,)
def foo(arg):, , .
, , Python . - . , . - , .
===================
reshape numpy/core/src/multiarray/methods.c ( github numpy). c
def reshape(self, *args, **kwargs):
n = len(args)
if n<=1:
newshape = <parse args[0] in one way>
else:
newshape = <parse args in another way>
return PyArray_Newshape(self, newshape, order)
, :
shape=(2,3)
np.arange(6).reshape(shape)
np.arange(6).reshape(*shape)
np.arange(6).reshape(2,3)
np.arange(6).reshape((2,3))
np.arange(6).reshape((2,)+(3,))