Contains/equals

Discussion in 'Python' started by Karsten Wutzke, Aug 19, 2010.

  1. 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
    Karsten Wutzke, Aug 19, 2010
    #1
    1. Advertising

  2. Karsten Wutzke

    Peter Otten Guest

    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
    Peter Otten, Aug 19, 2010
    #2
    1. Advertising

  3. On Aug 19, 4:47 pm, Peter Otten <> 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
    Karsten Wutzke, Aug 19, 2010
    #3
  4. Karsten Wutzke

    Alex Hall Guest

    On 8/19/10, Peter Otten <> 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.facebook.com/mehgcap
    Alex Hall, Aug 19, 2010
    #4
  5. 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
    Steven D'Aprano, Aug 19, 2010
    #5
  6. Karsten Wutzke

    Tim Chase Guest

    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
    Tim Chase, Aug 19, 2010
    #6
  7. In message <>, 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?
    Lawrence D'Oliveiro, Aug 24, 2010
    #7
  8. Lawrence D'Oliveiro <_zealand> writes:

    > In message <>, 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
    Hrvoje Niksic, Aug 24, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    637
  2. Edward A Thompson
    Replies:
    4
    Views:
    515
    Tony Morris
    Feb 11, 2004
  3. GeorgeH
    Replies:
    3
    Views:
    470
    Patricia Shanahan
    Oct 14, 2006
  4. StM
    Replies:
    4
    Views:
    1,736
  5. LastHope
    Replies:
    6
    Views:
    602
Loading...

Share This Page