1

I have created a Python class:

class calculator:
    
    def addition(self,x,y):
        added = x + y
        print(added)
        
    def subtraction(self,x,y):
        sub = x - y
        print(sub)

    def multiplication(self,x,y):
        mult = x * y
        print(mult)

    def division(self,x,y):
        div = x / y
        print(div)

Now when I am calling the function like this:

calculator.addition(2,3)

I am getting an error:

addition() missing 1 required positional argument: 'y'

What is the problem? What could be the solution so that I can call it like addition(2,3)?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Faraz Khan
  • 85
  • 1
  • 1
  • 7
  • 3
    The problem is that you have to either create instance of class or declare methods as static. – Olvin Roght Jul 25 '20 at 07:26
  • Read up on [TypeError: Missing one required positional argument](https://stackoverflow.com/a/50594457/7414759) and [Tutorial - 9.3. A First Look at Classes](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) – stovfl Jul 25 '20 at 08:02

5 Answers5

5

Python' classes have three types of methods:

  1. Instance method

The instance method must pass the instance object as the first parameter to the method, which is self, like:

class calculator:
    
    def addition(self, x, y):
        added = x + y
        print(added)

c = calculator()        
c.addition(2, 3)
  1. Class method

The class method use classmethod decorator and must pass the class object as the first parameter to the method, which is cls, like:

class calculator:
    
    @classmethod
    def addition(cls, x, y):
        added = x + y
        print(added)
    
calculator.addition(2, 3)
  1. Static method

The static method doesn’t matter, just add an staticmethod decorator, like:

class calculator:
    
    @staticmethod
    def addition(x, y):
        added = x + y
        print(added)
    
calculator.addition(2, 3)

So, the last two ways can be your answer if you just want to call with a class object like calculator.addition(2, 3).

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
K. Prot
  • 169
  • 4
3

You have to pass in the self variable by actually declaring an instance of this class, like this:

myCalculator = calculator()

myCalculator.addition(2,3)

Output should be: 5

de_classified
  • 1,927
  • 1
  • 15
  • 19
1

Please create a instance of the class first:

calculator_instance = calculator()

Then, you can call the function as calculator_instance.addition(2,3)

blutab
  • 181
  • 8
0
  1. You should use the name "Calculator" for your class
  2. When you call the function Caculator.addition you need 3 args (self, x, y) but you gived 2
  3. You should write your function «addition(self,x,y):» like this «addition(self, x: int, y: int):»

If you just want the function addition write it not as a class methode but as a function. If you need write «calculator.addition(2,3)» first do calculator = Calculator()

-1

You just neet to delete self. Self will be used when you have __init __ function

class calculator:
    
    def addition(x,y):
        added = x + y
        print(added)
        
    def subtraction(x,y):
        sub = x - y
        print(sub)

    def multiplication(x,y):
        mult = x * y
        print(mult)

    def division(x,y):
        div = x / y
        print(div)

calculator.addition(2,3)
Sahak Sahakyan
  • 101
  • 1
  • 9