我是一个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}$为什么在这里?这应该做什么?在
非常感谢:)
所讨论的类似乎是
list
的子类,具有一些附加属性,如name
,start
,等等,通过调用list.__eq__(self, other)
,显式调用__eq__
方法(而不是子类中定义的方法)来比较这两个对象。在检查了两个列表的其他属性是否相等之后,这可能会比较两个列表的内容。在通常,
cls.method(obj, *args)
相当于obj.method(*args)
,如果obj
是cls
的实例,但是在这种情况下,仅仅调用self.__eq__(other)
(或self == other
)就会再次调用相同的__eq__
方法,导致无限递归。在既然您说您来自Java:假设这个类是来自}类似。在
list
的子类,那么调用list.__eq__(self, other)
与调用{相关问题 更多 >
编程相关推荐