In NumPy, you can transpose a ndarray with the transpose() method, but it has an attribute, T, as well.
When a ndarray is altered, not only
the return value of transpose() method, but also the attribute T is automatically updated at the same time.
ndarray.transpose() is a function, so it is not to be wondered at.
However, T is not a function, but a mere attribute, so it must be updated before accessed.
Do all the NumPy functions and methods update the
Tattribute every time they are called?If so, why? It is likely that, when handling huge matrices, computational cost of every operation would be very large even if it is not relevant to transposition. Isn't it more reasonable to call
transpose()only when necessary instead of rewriting theTattribute every time?
This is a simple example:
>>> import numpy as np
>>> x = np.array([[1, 2, 3],
... [4, 5, 6]])
...
>>> x
array([[1, 2, 3],
[4, 5, 6]])
>>> x.T
array([[1, 4],
[2, 5],
[3, 6]])
Of course, operation on x simultaneously changes x.T as well. When was x.T updated? Did x.__imul__() do it?
>>> x *= 100
>>> x
array([[100, 200, 300],
[400, 500, 600]])
>>> x.T
array([[100, 400],
[200, 500],
[300, 600]])