By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Equal to Anything

P: n/a
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,
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Oct 4 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Simon Brunning" <si***@brunningonline.netwrote:
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?
Not in general, no. If you could, and someone else created a
NotEqualAnything class with the identical definition to EqualAnything
except it returns False, then which would win?

Sorry, but the left hand value wins unless it volunteers otherwise.
Oct 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.