Faraz Khan
Faraz Khan

Reputation: 85

Missing 1 required positional argument: 'y'

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)?

Upvotes: 1

Views: 12925

Answers (5)

K. Prot
K. Prot

Reputation: 169

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).

Upvotes: 5

Inlife and you too.
Inlife and you too.

Reputation: 36

  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()

Upvotes: 0

Sahak Sahakyan
Sahak Sahakyan

Reputation: 101

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)

Upvotes: -1

Data_Is_Everything
Data_Is_Everything

Reputation: 2017

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

Upvotes: 3

blutab
blutab

Reputation: 181

Please create a instance of the class first:

calculator_instance = calculator()

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

Upvotes: 1

Related Questions