Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Contains/equals

Reply
Thread Tools

Contains/equals

 
 
Karsten Wutzke
Guest
Posts: n/a
 
      08-19-2010
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-19-2010
Karsten Wutzke wrote:

> 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:

>>> class Cornucopia:

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

True
>>> "cherry pie" in c

True
>>> c in c

True

 
Reply With Quote
 
 
 
 
Karsten Wutzke
Guest
Posts: n/a
 
      08-19-2010
On Aug 19, 4:47*pm, Peter Otten <(E-Mail Removed)> wrote:
> Karsten Wutzke wrote:
> > 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:
>
> >>> class Cornucopia:

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

> True
> >>> "cherry pie" in c

> True
> >>> c in c

>
> True


Whoops that easy... Thanks!
Karsten
 
Reply With Quote
 
Alex Hall
Guest
Posts: n/a
 
      08-19-2010
On 8/19/10, Peter Otten <(E-Mail Removed)> wrote:
> Karsten Wutzke wrote:
>
>> 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:
>
>>>> class Cornucopia:

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

> True
>>>> "cherry pie" in c

> True
>>>> c in c

> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Have a great day,
Alex (msg sent from GMail website)
http://www.velocityreviews.com/forums/(E-Mail Removed); http://www.facebook.com/mehgcap
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-19-2010
On Thu, 19 Aug 2010 11:00:03 -0400, Alex Hall wrote:

> 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



--
Steven
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      08-19-2010
On 08/19/10 12:42, Steven D'Aprano wrote:
> On Thu, 19 Aug 2010 11:00:03 -0400, Alex Hall wrote:
>
>> 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


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



 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      08-24-2010
In message <(E-Mail Removed)>, Alex Hall
wrote:

> 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?
 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      08-24-2010
Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:

> In message <(E-Mail Removed)>, Alex Hall
> wrote:
>
>> 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?


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
 
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




Advertisments