1

I'm a python newbie and yesterday I had to dive into the source code of graphite-web open source project. But my question is not specifically about graphite.

I found this function:

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)

I understand what this method is supposed to do (I come from the java world, and this is quite familiar). My interrogation is about this sample, at the end :

list.__eq__(self, other)

Why is it here and what is this supposed to do ?

Many thanks :)

  • you can find related info [here](https://stackoverflow.com/questions/3588776/how-is-eq-handled-in-python-and-in-what-order) – Advay Umare Feb 08 '18 at 09:32
  • Without more context, one cannot say *why* it is there, but simply put, it is calling the `list.__eq__` method with the arguments `self` and `other`... does this class inherit from `list`? – juanpa.arrivillaga Feb 08 '18 at 09:33
  • This is from actual code of list class `""" x.__eq__(other) <==> x==other """`. General purpose is to check equality but can be for doing other task in your code – Arpit Solanki Feb 08 '18 at 09:41
  • `__eq__` method of list class is used to check if two lists are equal. `list.__eq__([1,2,3],[1,2,3])` produce `True` and `list.__eq__([1,2,3],[1,2,3,4])` produce `False` – Sohaib Farooqi Feb 08 '18 at 09:42

1 Answers1

3

The class in question seems to be a sub-class of list, with some additional attributes like name, start, etc., and by calling list.__eq__(self, other), you explicitly call the __eq__ method of list (instead of the one defined in the subclass) to compare the two objects. This will likely compare the content of the two lists after their other attributes have been checked for equality.

Usually, cls.method(obj, *args) is equivalent to obj.method(*args), if obj is an instance of cls, but in this case, just calling self.__eq__(other) (or self == other) would call the same __eq__ method again, resulting in an infinite recursion.

Since you said that you are coming from Java: Provided that this class is a subclass from list, calling list.__eq__(self, other) is similar to calling super.equals(other).

tobias_k
  • 81,265
  • 12
  • 120
  • 179