Skip to content Skip to sidebar Skip to footer

Pass Class Method To Fsolve

I have the following code: import scipy.optimize class demo(object): def get_square(self, var): return var ** 2 - 4 new = demo() scipy.optimize.fsolve(new.get_square(), 1) A

Solution 1:

You're actually calling the function before fsolve has a change to do anything; since the call has no arguments this will raise the expected TypeError.

You could either remove the call () to new.get_square:

scipy.optimize.fsolve(new.get_square, 1)

or, since you aren't actually even using self in get_square, make it a @staticmethod:

class demo(object):
  @staticmethod
  def get_square(var):
    return var ** 2 - 4

new = demo()
scipy.optimize.fsolve(new.get_square, 1)

Two small notes:

  • Use CapWords for class names, that is, demo -> Demo.
  • If you aren't trying to be portable between Python 2/3, no need to inherit from object.

Post a Comment for "Pass Class Method To Fsolve"