test whether 2 objects are equal

Discussion in 'Python' started by Yves Glodt, Jan 31, 2006.

  1. Yves Glodt

    Yves Glodt Guest

    Hello,


    I need to compare 2 instances of objects to see whether they are equal
    or not, but with the code down it does not work (it outputs "not equal")


    #!/usr/bin/python

    class Test:
    var1 = ''
    var2 = ''

    test1 = Test()
    test1.var1 = 'a'
    test1.var2 = 'b'

    test2 = Test()
    test2.var1 = 'a'
    test2.var2 = 'b'

    if test1 == test2:
    print "equal"
    else:
    print "not equal"




    What am I doing wrong...?


    best regards,
    Yves
    Yves Glodt, Jan 31, 2006
    #1
    1. Advertising

  2. Yves Glodt

    Rene Pijlman Guest

    Yves Glodt:
    >I need to compare 2 instances of objects to see whether they are equal
    >or not,


    This prints "equal":

    class Test(object):
    def __init__(self):
    self.var1 = ''
    self.var2 = ''
    def __eq__(self,other):
    return self.var1 == other.var1 and self.var2 == other.var2

    test1 = Test()
    test1.var1 = 'a'
    test1.var2 = 'b'

    test2 = Test()
    test2.var1 = 'a'
    test2.var2 = 'b'

    if test1 == test2:
    print "equal"
    else:
    print "not equal"

    --
    René Pijlman
    Rene Pijlman, Jan 31, 2006
    #2
    1. Advertising

  3. Yves Glodt

    Yves Glodt Guest

    Rene Pijlman wrote:
    > Yves Glodt:
    >> I need to compare 2 instances of objects to see whether they are equal
    >> or not,

    >
    > This prints "equal":


    thank you!

    Have a nice day,
    Yves

    > class Test(object):
    > def __init__(self):
    > self.var1 = ''
    > self.var2 = ''
    > def __eq__(self,other):
    > return self.var1 == other.var1 and self.var2 == other.var2
    >
    > test1 = Test()
    > test1.var1 = 'a'
    > test1.var2 = 'b'
    >
    > test2 = Test()
    > test2.var1 = 'a'
    > test2.var2 = 'b'
    >
    > if test1 == test2:
    > print "equal"
    > else:
    > print "not equal"
    >
    Yves Glodt, Jan 31, 2006
    #3
  4. Yves Glodt wrote:
    > Hello,
    >
    >
    > I need to compare 2 instances of objects to see whether they are equal
    > or not, but with the code down it does not work (it outputs "not equal")
    >
    >
    > #!/usr/bin/python
    >
    > class Test:
    > var1 = ''
    > var2 = ''


    Take care, this creates two *class* variables var1 and var2. For
    *instance* variables, you want:

    class Test:
    def __init__(self, var1='', var2=''):
    self.var1 = var1
    self.var2 = var2


    > test1 = Test()
    > test1.var1 = 'a'
    > test1.var2 = 'b'


    This creates instances variables var1 and var2 for test1 (shadowing
    class variables).

    (snip the rest, see other posts in this thread)

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno at modulix, Jan 31, 2006
    #4
  5. Yves Glodt

    Yves Glodt Guest

    bruno at modulix wrote:
    > Yves Glodt wrote:
    >> Hello,
    >>
    >>
    >> I need to compare 2 instances of objects to see whether they are equal
    >> or not, but with the code down it does not work (it outputs "not equal")
    >>
    >>
    >> #!/usr/bin/python
    >>
    >> class Test:
    >> var1 = ''
    >> var2 = ''

    >
    > Take care, this creates two *class* variables var1 and var2. For
    > *instance* variables, you want:


    Thanks for making me aware. I'll have to read more about classes in
    python... ( As you can see I'm still new to it ;-)

    btw, this is the best list I've ever joined, very helpful and nice ppl.

    Have a nice day!
    Yves

    > class Test:
    > def __init__(self, var1='', var2=''):
    > self.var1 = var1
    > self.var2 = var2
    >
    >
    >> test1 = Test()
    >> test1.var1 = 'a'
    >> test1.var2 = 'b'

    >
    > This creates instances variables var1 and var2 for test1 (shadowing
    > class variables).
    >
    > (snip the rest, see other posts in this thread)
    >
    Yves Glodt, Jan 31, 2006
    #5
  6. Yves Glodt a écrit :
    > bruno at modulix wrote:
    >
    >> Yves Glodt wrote:
    >>

    (snip)

    >>> #!/usr/bin/python
    >>>
    >>> class Test:
    >>> var1 = ''
    >>> var2 = ''

    >>
    >>
    >> Take care, this creates two *class* variables var1 and var2. For
    >> *instance* variables, you want:

    >
    >
    > Thanks for making me aware. I'll have to read more about classes in
    > python... ( As you can see I'm still new to it ;-)


    I don't remember what's your background, but if you come from
    C++/Java/..., then yes, definitevely, you need to learn more about
    Python's object model. The class variable/instance variable confusion is
    a common gotcha.

    BTW, better use 'new-style' classes (that is - for short-, classes that
    inherit at least from 'object' or any other new-style class), ie:

    class NewStyle(object):
    # this is a new-style class
    pass

    class NewStyleToo(NewStyle):
    # this is a new-style class too
    pass

    class OldStyle:
    # this is a deprecated old-style class


    > btw, this is the best list I've ever joined, very helpful and nice ppl.


    Yes, this is definitively a nice place !-)
    Bruno Desthuilliers, Feb 1, 2006
    #6
    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. Porthos
    Replies:
    1
    Views:
    2,159
    Peter Flynn
    Jan 3, 2005
  2. Jeff
    Replies:
    11
    Views:
    1,500
    dorayme
    Jan 23, 2008
  3. Slaunger
    Replies:
    11
    Views:
    397
    Steven D'Aprano
    Aug 8, 2008
  4. Frederik
    Replies:
    5
    Views:
    967
    lewbloch
    Jul 20, 2011
  5. PerlFAQ Server
    Replies:
    2
    Views:
    665
    Dr.Ruud
    Apr 22, 2011
Loading...

Share This Page