2

I'm just starting to learn Python, and was writing a calculator program with two different files to get the concept of calling classes and functions down. The file with the mathmatical operations in a class looks as such:

class standardOpperations:
def __init__(self, primaryNumber, secondaryNumber):
    primaryNumber.self = primaryNumber
    secondaryNumber.self = secondaryNumber
def addition(self, primaryNumber, secondaryNumber):
    print(primaryNumber, "+", secondaryNumber, "=", primaryNumber.self+secondaryNumber)
def subtraction(self, primaryNumber, secondaryNumber):
    print(primaryNumber, "-", secondaryNumber, "=", primaryNumber-secondaryNumber)
def multiplication(self, primaryNumber, secondaryNumber):
    print(primaryNumber, "*", secondaryNumber, "=", primaryNumber * secondaryNumber)
def division(self, primaryNumber, secondaryNumber):
    print(primaryNumber, "/", secondaryNumber, "=", primaryNumber / secondaryNumber)

In a separate file, I tried to call this object (specifically the first function) in this line of code:

number01 = float(input("Please input a number here: "))
number02 = float(input("Please input a second number here: "))
addObj = standardOpperations.addition(number01,number02)

My issue is that upon running it, I get an error stating that I am missing a required potential argument, even though the function only uses two numbers. I'd really appreciate any help I can get in spotting my mistake, and helping the program work. Thanks all!

1
  • hint: you're calling an instance method like a normal function (note the required self argument in instance methods - this self argument is only automatically and implicitly passed as the first argument when called as instance method) Commented Feb 23, 2020 at 6:13

2 Answers 2

1

Since you are treating each function as a static method, remove the constructor and the word self anywhere in the class.

class standardOpperations:

    def addition(primaryNumber, secondaryNumber):
        print(primaryNumber, "+", secondaryNumber, "=", primaryNumber+secondaryNumber)
    def subtraction(primaryNumber, secondaryNumber):
        print(primaryNumber, "-", secondaryNumber, "=", primaryNumber-secondaryNumber)
    def multiplication(primaryNumber, secondaryNumber):
        print(primaryNumber, "*", secondaryNumber, "=", primaryNumber * secondaryNumber)
    def division(primaryNumber, secondaryNumber):
        print(primaryNumber, "/", secondaryNumber, "=", primaryNumber / secondaryNumber)


>>> standardOpperations.addition(1,2)
1 + 2 = 3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That solved my problem. Now I have to ask though, what does the init function do for a function? I was taught to use that whenever I made a function in class, but there are evidently places where it shouldn't be used. Is there a specific purpose it is typically used for? Thank you again for helping me solve my issue
1

I have corrected your code.

Please find the working code below

class standardOpperations:
  def __init__(self, primaryNumber, secondaryNumber):
      self.primaryNumber = primaryNumber
      self.secondaryNumber = secondaryNumber
  def addition(self):
      print(self.primaryNumber, "+", self.secondaryNumber, "=", self.primaryNumber+self.secondaryNumber)
  def subtraction(self):
      print(self.primaryNumber, "-", self.secondaryNumber, "=", self.primaryNumber-self.secondaryNumber)
  def multiplication(self):
      print(self.primaryNumber, "*", self.secondaryNumber, "=", self.primaryNumber * self.secondaryNumber)
  def division(self):
      print(self.secondaryNumber, "=", self.primaryNumber / self.secondaryNumber)
number01 = float(input("Please input a number here: "))
number02 = float(input("Please input a second number here: "))
addObj = standardOpperations(number01,number02)
addObj.addition()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.