Re: test whether 2 objects are equal
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>
.
Relevant Pages
- Re: test whether 2 objects are equal
... Yves Glodt: ... >I need to compare 2 instances of objects to see whether they are equal ... test1 = Test ... test2 = Test ... (comp.lang.python) - How to test if an object IS another object?
... If two objects are of equal value you can compare them with ==. ... want to do is find out if two objects are actually just references to ... how can I do this in Python? ... Prev by Date: ... (comp.lang.python) - Re: Comparing two book chapters (text files)
... I want to compare two book ... I have advanced beginner python skills but am not quite ... Marc-Andre Lemburg ... Professional Python Services directly from the Source ... (comp.lang.python) - Re: Comparing files in a zip to files on drive
... Look at the Python docs for the os and zipfile modules. ... Zip files have the CRC of each file for checksumming purposes. ... You can calculate the CRC for the file on the hard drive and then compare the CRC values. ... A better plan would be to store a list of MD5 or SHA1 hashes for each file in your archive. ... (comp.lang.python) - Re: key argument for max?
... If you _want_ to compare items, when they have the same count, you ... Unfortunately being explicit _and_ ... > the Python sources before, and it's been a long time since I've ... but such a patch would not be accepted for Python ... (comp.lang.python) |
|