I want to implement my own matrix-class that inherits from numpy's matrix class.
numpy's matrix constructor requires an attribute, something like ("1 2; 3 4'")
. In contrast, my constructor should require no attributes and should set a default attribute to the super-constructor.
That's what I did:
import numpy as np
class MyMatrix(np.matrix):
def __init__(self):
super(MyMatrix, self).__init__("1 2; 3 4")
if __name__ == "__main__":
matrix = MyMatrix()
There must be a stupid mistake in this code since I keep getting this error:
this_matrix = np.matrix()
TypeError: __new__() takes at least 2 arguments (1 given)
I'm really clueless about that and googling didn't help so far.
Thanks!
Good question!
From looking at the source, it seems as though
np.matrix
sets thedata
argument in__new__
, not in__init__
. This is counterintuitive behaviour, though I'm sure there's a good reason for it.Anyway, the following works for me:
Addendum: you might want to consider using a factory function, rather than a subclass, for the behaviour you want. This would give you the following code, which is much shorter and clearer, and doesn't depend on the
__new__
-vs-__init__
implementation detail:Of course, you might need a subclass for other reasons.