having an error : "missing 1 required positional argument", even though i am entering the argument
Question:
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"
Answers:
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.
Alternatively, if you want the sum class to be like a Library of mathematical functions you could declare the methods as @classmethods:
class Sum:
@classmethod
def fx(cls, op, a, b, c, d):
if op == 1:
output = cls.addition(a, b, c, d)
else:
output = cls.subtraction(a, b, c, d)
return output
@classmethod
def addition(cls, a, b, c, d):
return a+b+c+d
@classmethod
def subtraction(cls, a, b, c, d):
return a-b-c-d
x = Sum.fx(1, 1, 2, 3, 4)
print(x)
This way you do not need to create a class instance, just use the functions
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.
Alternatively, if you want the sum class to be like a Library of mathematical functions you could declare the methods as @classmethods:
class Sum:
@classmethod
def fx(cls, op, a, b, c, d):
if op == 1:
output = cls.addition(a, b, c, d)
else:
output = cls.subtraction(a, b, c, d)
return output
@classmethod
def addition(cls, a, b, c, d):
return a+b+c+d
@classmethod
def subtraction(cls, a, b, c, d):
return a-b-c-d
x = Sum.fx(1, 1, 2, 3, 4)
print(x)
This way you do not need to create a class instance, just use the functions