473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

__eq__ and __ne__

I was surprised by the following behavior. Apparently, the "!="
operator does not fall back to using "not __eq__()". I tested this with
Python 2.1, 2.2, and 2.2 with new-style classes and got the same results
in every case.
class foo: .... def __eq__(self, other):
.... return (other.__class__ is self.__class__
.... and other.__dict__ == self.__dict__)
.... foo() == foo() 1 foo() != foo()

1

I would expect the second test to yield "0". To compensate, I've
started adding the following boilerplate code to all classes that define
only __eq__:

def __ne__(self, other):
return not self.__eq__(other)

Does this surprise anyone else? I have not yet found documentation on this.

Shane
Jul 18 '05 #1
0 2772

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
1
by: David Vaughan | last post by:
I was expecting the class sets.Set to act like an unordered list with no two members equal. But, while the following code prints True, the assertion fails. from sets import Set _base = str...
1
by: Roman Yakovenko | last post by:
> From: Alex Martelli > > Roman Yakovenko <roman.yakovenko@actimize.com> wrote: > > > Thanks. I have an other question, more general: > > > > I have class A that defines __eq__ and __ne__....
49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
45
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
9
by: Neil Benn | last post by:
Hello, I can't find the docs for __eq__ on a dict and I can't find a description on what the eq does (strangely it does implement > and < but I have no idea what that does). Does anyone know...
9
by: Emanuele Aina | last post by:
I have some code which does a lot of "in" on lists containing objects with no __eq__ defined. It all goes fast until I add the __lt__() method: then I have a slowdown comparable to the one I get...
18
by: jordan.nick | last post by:
Has the addition of a Universe Set object ever been suggested. Like U = set(0), so that any object was a member of U? Maybe this gets into some crazy Cantorian stuff since U is in U. But it...
270
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.