“列表”应该做什么?

2024-09-30 01:27:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个python新手,昨天我不得不深入研究graphite web开源项目的源代码。但我的问题不是关于石墨的。在

我发现了这个函数:

def __eq__(self, other):
  if not isinstance(other, TimeSeries):
    return False

  if hasattr(self, 'color'):
    if not hasattr(other, 'color') or (self.color != other.color):
      return False
  elif hasattr(other, 'color'):
    return False

  return ((self.name, self.start, self.end, self.step, self.consolidationFunc, self.valuesPerPoint, self.options, self.xFilesFactor) ==
  (other.name, other.start, other.end, other.step, other.consolidationFunc, other.valuesPerPoint, other.options, other.xFilesFactor)) and list.__eq__(self, other)

我知道这个方法应该做什么(我来自java世界,这是非常熟悉的)。我的审问是关于这个样本,最后:

^{pr2}$

为什么在这里?这应该做什么?在

非常感谢:)


Tags: nameselffalsereturnifstepnotstart
1条回答
网友
1楼 · 发布于 2024-09-30 01:27:55

所讨论的类似乎是list的子类,具有一些附加属性,如namestart,等等,通过调用list.__eq__(self, other),显式调用__eq__方法(而不是子类中定义的方法)来比较这两个对象。在检查了两个列表的其他属性是否相等之后,这可能会比较两个列表的内容。在

通常,cls.method(obj, *args)相当于obj.method(*args)如果objcls的实例,但是在这种情况下,仅仅调用self.__eq__(other)(或self == other)就会再次调用相同的__eq__方法,导致无限递归。在

既然您说您来自Java:假设这个类是来自list的子类,那么调用list.__eq__(self, other)与调用{}类似。在

相关问题 更多 >

    热门问题