繁体   English   中英

TypeError:+不支持的操作数类型:“ int”和“ IntervalMap”

[英]TypeError: unsupported operand type(s) for +: 'int' and 'IntervalMap'

达到了向现有项目-IntervalMap项目 !添加功能的目标。 由于尚未实现内部运算符( __add____mul__等),因此需要订阅它们。

这是我在__add__内置实现的代码,用于执行间隔图添加。

# ...

def __add__(self, other):
    """
    a = x + y
    """

    aux = intervalmap()

    if isinstance(other, self.__class__):

        ruler = self._bounds + other._bounds
        ruler = map(ruler.sort(), ruler)

        for i, x in enumerate(ruler):

            if i > 0:
                _slice = slice(ruler[i-1], x, None)
                point = (_slice.start + _slice.stop) / 2.0

                if self[point] is None:
                    aux.__setitem__(_slice, other[point])
                elif other[point] is None:
                    aux.__setitem__(_slice, self[point])
                else:
                    aux.__setitem__(_slice, self[point] + other[point])

    if isinstance(other, (int,float)):

        for i, x in enumerate(self._bounds):

            if i > 0:
                point = (self._bounds[i-1] + x) / 2
                aux[self._bounds[i-1]:x] = self[point] + other

    return aux 

 # ...

 if __name__ == '__main__':
      x = intervalmap()
      y = intervalmap()

      x[1:2] = 6
      x[4:5] = 1
      x[7:8] = 5

      y[0:3] = 4
      y[3:6] = 2
      y[6:9] = 11

      print x
      print y

产量

>>> {[1, 2] => 6, [4, 5] => 1, [7, 8] => 5}

>>> {[0, 3] => 4, [3, 6] => 2, [6, 9] => 11}

          a = x + y
          b = y + x
          print a
          print b


>>> {[0, 1] => 4, [1, 2] => 10, [2, 3] => 4, [3, 4] => 2, [4, 5] => 3, [5, 6] => 2, [6, 7] => 11, [7, 8] => 16, [8, 9] => 11}

>>> {[0, 1] => 4, [1, 2] => 10, [2, 3] => 4, [3, 4] => 2, [4, 5] => 3, [5, 6] => 2, [6, 7] => 11, [7, 8] => 16, [8, 9] => 11}

          a = y + 2
          print a
          b = 2 + y
          print b

>>> {[0, 3] => 6, [3, 6] => 4, [6, 9] => 13}

Traceback (most recent call last): File "/home/pc/workspace/simulador-mti/source/y.py", line 73, in <module> b = 2 + y

TypeError: unsupported operand type(s) for +: 'int' and 'IntervalMap'

希望无论向左还是向右操作数,都向间隔图对象添加一个常数。 如何将数字添加为左操作数?

顺便说一句,有没有人看到一种通用的方法? 就像传递给更新函数x.update(y, lambda x,y: x+y)执行相同的操作一样。

除了__add__之外,您还可以定义__radd__方法。

解释文档

__radd__ is only called if the left operand does not support the add operation 
and the operands are of different types. [2] 
For instance, to evaluate the expression x + y, where y is an instance of a class 
that has __radd__() method, 
y.__radd__(x) is called if x.__add__(y) returns NotImplemented.

它看起来应该像这样:

class X(object):
    def __init__(self, val):
        self.val = val

    def __add__(self, other):
        if isinstance(other, X):
            return X(self.val + other.val)
        else:
            return X(self.val + other)

    def __radd__(self, other):
        return self.__add__(other)

X(3) + 4 # calls `__add__`
4 + X(3) # calls `__radd__`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM