tags:

views:

78

answers:

1

Hello

I have a class that acts as an item in a tree:

class CItem( list ):
  pass

I have two trees, each with CItem as root, each tree item has some dict members (like item._test = 1). Now i need to compare this trees. I can suggest to overload a comparison operator for CItem:

class CItem( list ):
  def __eq__( self, other ):
    # first compare items as lists
    if not list.__eq__( self, other ): return False
    # now compare dict members
    if self.__dict__ != other.__dict__: return False
    # seems equal
    return True

Now i can compare two trees using '==' or '!='. Is this a 'pythonic' way or such comparsion can be done easire?

+3  A: 

My feeling would be something like

class CItem(list):
    def __eq__(self, other):
        return list.__eq__(self, other) and self.__dict__ == other.__dict__

but it's basically the same code you have, just expressed in shorter notation. I can't think of any more substantial changes to make offhand.

David Zaslavsky