Search code examples
pythonpython-3.xclass-method

Missing 2 required positional arguments - Classmethod Python


Sorry, I'm struggling at this for quite some time. I'm trying to use function totalPayments which uses the monthlyPayment class function with parameters pass through in the initialisation phase. I'm getting an error missing 2 required positional arguments

class Loan(object):
   def __init__(self, asset, face, rate , term):
       self._asset = asset
       self._face = face
       self._rate = rate
       self._term = term

   @classmethod
   def monthlyPayment(cls,face,rate,term,period=None):
       return ((rate*face*((1+rate)**term)))/(((1+rate)**term)-1)

   def totalPayments(self):
       return (self.monthlyPayment(self) * self._term) 

l = Loan(None,1000,0.025,10)
print(l.totalPayments()) # gets an error missing 2 required positional arguments

edit: Thank you very much for the hekp and I should be amending my def monthlyPayment function to take in the arguments


Solution

  • You are calling monthlyPayment from the instance (self), and you're not providing arguments for face, rate, and term.

    It also shouldn't be a classmethod, since you use attributes of the instance:

    class Loan(object):
       def __init__(self, asset, face, rate , term):
           self._asset = asset
           self._face = face
           self._rate = rate
           self._term = term
    
       def monthlyPayment(self, period=None):
           return ((self._rate*self._face*((1+self._rate)**self._term)))/(((1+self._rate)**self._term)-1)
    
       def totalPayments(self):
           return (self.monthlyPayment() * self._term)