class sum:
def fx(self, op, a, b, c, d):
if(op == 1):
self.output = self.addition(a, b, c, d)
else:
self.output = self.subtraction(a, b, c, d)
def addition(self, a, b, c, d):
return a+b+c+d
def subtraction(self, a, b, c, d):
return a-b-c-d
x = sum.fx(1, 1, 2, 3, 4)
The above code gives an error
x = sum.fx(1, 1, 2, 3, 4) TypeError: sum.fx() missing 1 required positional argument: 'd'
I am clearly entering the value parameter 'd' but it says that i am not. It should give an output "10"
It should be sum().fx(...)
. The first argument you passed is considered to be the instance of the class (self
) but if we consider that then you are missing one of the arguments d
that you need to pass?
You should instantiate in this case first to call the methods.
Note: By using the self
we can access the attributes and methods of the class in python. In your case, even if you provide extra random arguments .fx(1,1,2,3,4)
it would run into error later. Because you don't have the instance of the class.