Add __eq__ and __ne__ methods to Tree.
authorW. Trevor King <wking@drexel.edu>
Sat, 5 Dec 2009 05:21:35 +0000 (00:21 -0500)
committerW. Trevor King <wking@drexel.edu>
Sat, 5 Dec 2009 05:21:35 +0000 (00:21 -0500)
This fixes a bug introduced by
  revision-id: wking@drexel.edu-20091205034412-8apqxq8zqim48tf7
  committer: W. Trevor King <wking@drexel.edu>
  timestamp: Fri 2009-12-04 22:44:12 -0500
  message:
    Use __cmp__ instead of __eq__ for Tree comparison.

When I made that commit, I'd forgotten that Tree inherits an __eq__
method from list, so it won't fall back to the __cmp__ method to
determine equality.  The new __eq__ and __ne__ methods use __cmp__
internally, so further subclasses (e.g. Comment) only need to override
__cmp__.  Of course, list also defines __ge__, __gt__, __le__, __lt__,
... which I don't bother with, so stay away from TreeA > TreeB and the
like.

libbe/tree.py

index d3f6bcd5d4f0b835d518ea256f9812882eeef1be..1daac44560fa008deb55ea266f9ea795fa8f8a90 100644 (file)
@@ -87,6 +87,12 @@ class Tree(list):
     def __cmp__(self, other):
         return cmp(id(self), id(other))
 
+    def __eq__(self, other):
+        return self.__cmp__(other) == 0
+
+    def __ne__(self, other):
+        return self.__cmp__(other) != 0
+
     def branch_len(self):
         """
         Exhaustive search every time == SLOW.