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?
From stackoverflow
-
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.
0 comments:
Post a Comment