Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Equal to Anything

Reply
Thread Tools

Equal to Anything

 
 
Simon Brunning
Guest
Posts: n/a
 
      10-04-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,
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      10-04-2006
"Simon Brunning" <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
driver irql not less or equal =?Utf-8?B?Y2VudHVyaWFu?= Wireless Networking 2 03-11-2005 08:30 PM
Load sharing between links where AS path length is equal Andrew Gideon Cisco 5 03-01-2005 11:55 PM
How to allocate an equal share of incoming web traffic to every user Anton Panyushkin Cisco 4 11-23-2004 02:41 PM
Re: OSPF Equal cost balancing in serie 2500 routers Aaron Woody Cisco 0 10-20-2003 02:42 PM



Advertisments