Say I have a namedtuple
like this:
FooTuple = namedtuple("FooTuple", "item1, item2")
And I want the following function to be used for hashing:
foo_hash(self):
return hash(self.item1) * (self.item2)
I want this because I want the order of item1
and item2
to be irrelevant (I will do the same for the comparison-operator). I thought of two ways to do this. The first would be:
FooTuple.__hash__ = foo_hash
This works, but it feels hacked. So I tried subclassing FooTuple
:
class EnhancedFooTuple(FooTuple):
def __init__(self, item1, item2):
FooTuple.__init__(self, item1, item2)
# custom hash function here
But then I get this:
DeprecationWarning: object.__init__() takes no parameters
So, what can I do? Or is this a bad idea altogether and I should just write my own class from scratch?
repr(foo)
will still be talking ofFoo
. This could be done better asclass Foo(namedtuple('Foo', ['item1', 'item2'], verbose=False)):
– Sheriesherif