Equal to Anything

Simon Brunning simon at brunningonline.net
Wed Oct 4 11:17:08 CEST 2006


For the purposes of a little test utility that we use, I need an
object that evaluates as equal to anything. (I realise that this is a
bad idea in some ways, breaking the equals/hashcode invariant and so
forth, but I'm hoping that I can get away with it in this case.) It
seems a simple enough task at first:

>>> class EqualAnything(object):
...     def __eq__(self, other):
...         return True
...
>>> spam = EqualAnything()
>>> spam == 1
True
>>> spam == "hello!"
True
>>> spam == datetime.datetime.now()
True
>>> 1 == spam
True
>>> "hello!" == spam
True

But...

>>> datetime.datetime.now() == spam
False

I'm fairly sure that I know what is going on here - the left hand side
object is getting first stab at the equality test, and understandably,
it's saying "Nah". But is there anything that I can do about it?

-- 
Cheers,
Simon B,
simon at brunningonline.net
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list