Contains/equals

K

Karsten Wutzke

Hello,

I have an object which has a list of other complex objects. How do I
best achieve a complex "contains" comparison based on the object's
class? In Java terms, I'm looking for an equivalent to equals(Object)
in Python. Does a similar thing exist? Directions appreciated.

Karsten
 
P

Peter Otten

Karsten said:
I have an object which has a list of other complex objects. How do I
best achieve a complex "contains" comparison based on the object's
class? In Java terms, I'm looking for an equivalent to equals(Object)
in Python. Does a similar thing exist? Directions appreciated.

I can't infer from your question whether you already know about
__contains__(). So there:
.... def __contains__(self, other):
.... return True
....True
 
K

Karsten Wutzke

I can't infer from your question whether you already know about
__contains__(). So there:


...     def __contains__(self, other):
...             return True
...>>> c = Cornucopia()

True

Whoops that easy... Thanks!
Karsten
 
A

Alex Hall

I can't infer from your question whether you already know about
__contains__(). So there:

... def __contains__(self, other):
... return True
...
True
You may also want to use the double equals operator; in Java, one
thing I wish I could do is
String s="abc";
String t="def";
if(s==t)...
In Python, as I understand it, you can define this behavior.

class c(object):
def __init__(self, a=1, b=2):
self.a=a; self.b=b

def __eq__(self, obj):
if self.a==obj.a and self.b==obj.b: return True
return False


You can now say:
obj1=c()
obj2=c()
print(obj1==obj2) #prints True
 
S

Steven D'Aprano

def __eq__(self, obj):
if self.a==obj.a and self.b==obj.b: return True
return False


That would be the same as:

def __eq__(self, obj):
return self.a==obj.a and self.b==obj.b
 
T

Tim Chase

That would be the same as:

def __eq__(self, obj):
return self.a==obj.a and self.b==obj.b

Or, if you have lots of attributes and 2.5+

def __eq__(self, other):
return all(
getattr(self, attr) == getattr(other, attr)
for attr in ['a', 'b', 'c', ...]
)

or even something like

def __eq__(self, other):
return all(
getattr(self, attr) == getattr(other, attr)
for attr in dir(self)
if not attr.startswith("__") and not attr.endswith("__")
)



-tkc
 
L

Lawrence D'Oliveiro

Alex Hall said:
def __eq__(self, obj):
if self.a==obj.a and self.b==obj.b: return True
return False

Is there a “Useless Use Of ...†award category for these “if <boolean> then
return True; else return False†constructs?
 
H

Hrvoje Niksic

Lawrence D'Oliveiro said:
Is there a “Useless Use Of ...” award category for these “if <boolean> then
return True; else return False” constructs?

Well, remember that self.a == obj.a can return something other than
bool, and the and operator will evaluate to either False or the last
value. Maybe he doesn't want to propagate the non-bools out of his
__eq__. :)

what's-next-"useless-use-of-"useless-use-of...""-ly y'rs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top