Skip to content
Advertisement

Initializing a class with numpy array question

So I have defined the following function to be entered into a class:

JavaScript

Which works exactly how I want it to, taking in a numpy array as argument, and produces a simpler matrix as a numpy array.

Now, I would like to define a class that is initialized by a numpy array, and once I enter a matrix into the class, called SeveralConvertor, to run the entered matrix through MatrixConverter, thus creating an internal representation of the converted matrix. My attempt at this goes as follows:

JavaScript

Letting q be some random array, and then typing q.MatrixConverter gives the following error:

JavaScript

Now, as I said, the function MatrixConverter(arr) works fine as a function, but when I entered it into the class, I exchange all arr for self, which might have something to do with the problem.

Help would be much appriciated!

Advertisement

Answer

No need to do anything too fancy, we can bind the (unit tested) function to a class method, then invoke that in the __init__ function.

JavaScript

Then in your code (put the above in a module and import the class)

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement