Re: test whether 2 objects are equal
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: test whether 2 objects are equal

From: Fredrik Lundh <fredrik@pythonware.com>
Date: Tue Jan 31 2006 - 11:08:44 CET

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>
Received on Tue Feb 7 20:19:36 2006