test whether 2 objects are equal

Fredrik Lundh fredrik at pythonware.com
Tue Jan 31 05:08:44 EST 2006


Yves Glodt wrote

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

you haven't told Python how you want Test instances to be compared.

    http://docs.python.org/ref/comparisons.html

    http://docs.python.org/ref/customization.html
    (look for __lt__ etc, as well as __cmp__ )

</F> 






More information about the Python-list mailing list