The questions you're asking don't really have an answer, because the case you're asking about simply doesn't exist.
Typically, in Python, you do not have the same function available as a method and as a global function.
NumPy is a special case, because some, but not all, top-level functions are also available as methods on the appropriate object. Even then, they often don't have the same semantics, so the answer isn't a question of style, but of which one is the right function.
For example, in your case, the only one you have a choice on is diagonal
. And the two options give different results.
>>> m = matrix([[1,2,3], [4,5,6], [7,8,9]]
>>> np.diagonal(m)
array([1, 5, 9])
>>> m.diagonal()
matrix([[1, 5, 9]])
The module function takes a 2D array of shape (N, N) and returns a 1D array of shape (N,). The method takes a 2D matrix of shape (N, N) and returns a 2D matrix of shape (1, N).
It's possible that the matrix
method will be faster. But that's not as important as the fact that if one of them is correct, the other one is wrong. It's like asking whether +
or *
is a faster way to multiply two numbers. Whether +
is faster than *
or not, it's not a faster way to multiply, because it doesn't multiply.