0

我正在做一个通用的重载运算符函数,它将接受运算符 +、- 、、/ 和 *。运算符以变量“op”的形式出现。我的问题是除以零。第二个 elif 语句是我试图这样做的地方,但它并不完全正确。我要介绍的是两件事:self.list[L][0] == operand.list[R][0]如果右侧操作数在除法时不等于零(即op == '/'operand.list[R][1] != 0),则允许条件为真。如果它是错误的,那么它只是转到 else 语句。

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
4

2 回答 2

1

在你的类中,你需要显式地重载操作符:、、、和__add__你的自定义行为。否则不会调用您的数学函数。__sub____mul____div__

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)

现在你可以像这样使用它:

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)

显然这是一个玩具示例;我不建议使用这样的类——用一个大的负前哨处理除以零可能会在以后导致问题。

于 2013-06-01T05:52:45.973 回答
1

您可能需要将所有or子句放在括号中。

由于and优先级高于or,因此您的子句:

(self.list[L][0] == operand.list[R][0]) and op == '*' or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0)

被评估为

((self.list[L][0] == operand.list[R][0]) and op == '*') or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0)

所以将其更改为

(self.list[L][0] == operand.list[R][0]) and (op == '*' or op == '**' or op == '+' or op == '-' or (op == '/' and operand.list[R][1] != 0))

应该做的伎俩。或者,如果您确定op只能是您提到的其中之一:

(self.list[L][0] == operand.list[R][0]) and (op != '/' or operand.list[R][1] != 0)

编辑

从评论中,我坚信您的操作员测试不正确。如果op是一个内置函数,如div,你应该这样做:

from operator import div, mul, pow, add, sub

在你的条件句中

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

或者,

(self.list[L][0] == operand.list[R][0]) and (op != div or operand.list[R][1] != 0)
于 2013-06-01T05:08:49.300 回答