I’m trying to pass a numpy matrix to an object method but I keep getting TypeError: test_obj() takes exactly 1 argument (2 given)
I think the matrix object is not getting interpreted properly as a matrix object, however when the same code is run as simple function it works fine. How can I get my object method to work like the simple function?
code:
from numpy import *
class Tester(object):
def test_obj(x):
print 'test obj:', type(x)
def test_fun(x):
print 'test fun:', type(x)
X = matrix('5.0 7.0')
test_fun(X)
tester = Tester()
tester.test_obj(X)
output:
test fun: <class 'numpy.matrixlib.defmatrix.matrix'>
Traceback (most recent call last):
File "/home/fornarim/test_matrix.py", line 22, in <module>
tester.test_obj(X)
TypeError: test_obj() takes exactly 1 argument (2 given)
All objects’ methods take an implicit self argument, so your method test_fun must be
Unlike in Java, in Python you must refer back to the object.
As mentioned below, also possible is using the @staticmethod decorator to indicate that the function does not need a reference to the object.