Reputation: 79
I'm doing a generic overloaded operator function that will accept operators +, -, , /, and *. The operator comes in as variable "op". My problem is catching the divide by zero. The second elif statement is where I'm trying to do this, but it isn't quite correct. What I'm trying to cover is two things: allow the condition to be true if self.list[L][0] == operand.list[R][0]
and secondly if the right hand side operand doesn't equal zero when it's division (i.e. op == '/'
and operand.list[R][1] != 0
). If it's false, then it just goes to the else statement.
def math(self, op, operand):
obj = Object()
L, R = 0, 0
while (L < len(self.list) and R < len(operand.list)):
if self.list[L][0] > operand.list[R][0]:
R += 1
elif self.list[L][0] < operand.list[R][0]:
L += 1
elif (self.list[L][0] == operand.list[R][0]) and op == '*' or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0):
obj.append(self.list[L][0], op(self.list[L][1], operand.list[R][1]))
L += 1
R += 1
else:
L += 1
R += 1
return obj
Upvotes: 0
Views: 146
Reputation: 5374
You probably need to enclose all of your or
clauses together in parentheses.
Since and
has higher precedence than or
, your clause :
(self.list[L][0] == operand.list[R][0]) and op == '*' or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0)
gets evaluated as
((self.list[L][0] == operand.list[R][0]) and op == '*') or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0)
So changing it to
(self.list[L][0] == operand.list[R][0]) and (op == '*' or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0))
should do the trick. Or if you're sure op
can only be one of those you mentionned:
(self.list[L][0] == operand.list[R][0]) and (op != '/' or operand.list[R][1] != 0)
EDIT :
From the comments, I strongly believe your operator testing isn't right. If op
is a built-in function like div
, here's what you should do:
from operator import div, mul, pow, add, sub
and in your conditional clause
(self.list[L][0] == operand.list[R][0]) and (op == mul or op == pow or op == add or op == sub or (op == div and operand.list[R][1] != 0))
or,
(self.list[L][0] == operand.list[R][0]) and (op != div or operand.list[R][1] != 0)
Upvotes: 1
Reputation: 17761
In your class, you need to explicitly overload the operators: __add__
, __sub__
, __mul__
, and __div__
with your custom behaviors. Your math function won't be called otherwise.
class LoudObject(object):
def __init__(self, val):
""" How to initialize the object with a value """
self.val = val
def __repr__(self):
""" How to represent the value when displayed """
return "LoudObject(%d)" % (self.val,)
def __add__(self, other):
print "Look! Addition!"
return LoudObject(self.val + other.val)
def __sub__(self, other):
print "Subtraction!"
return LoudObject(self.val - other.val)
def __mul__(self, other):
print "Multiplication!!!"
return LoudObject(self.val * other.val)
def __div__(self, other):
print "Division!"
if other.val == 0:
print "uh-oh, division by zero. No idea what I should do."
print "Maybe no one will notice if I return -99999999"
return LoudObject(-99999999)
return LoudObject(self.val / other.val)
Now you can use it like:
In [2]: LoudObject(3) + LoudObject(4)
Look! Addition!
Out[2]: LoudObject(7)
In [3]: LoudObject(3) / LoudObject(4)
Division!
Out[3]: LoudObject(0)
In [4]: LoudObject(3) / LoudObject(4.0)
Division!
Out[4]: LoudObject(0.75)
In [5]: LoudObject(3) / LoudObject(0)
Division!
uh-oh, division by zero. No idea what I should do.
Maybe no one will notice if I return -99999999
Out[5]: LoudObject(-99999999)
Obviously this is a toy example; I wouldn't suggest using a class like this -- and handling division by zero by a large negative sentinel is probably going to lead to problems later on.
Upvotes: 1