<div class="gmail_quote">On Thu, May 12, 2011 at 6:23 AM, Roy Smith <span dir="ltr"><<a href="mailto:roy@panix.com">roy@panix.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I have a vague feeling this may have been discussed a long time ago, but<br>
I can't find the thread, so I'll bring it up again.<br>
<br>
I recently observed in the "checking if a list is empty" thread that a<br>
list and a subclass of list can compare equal:<br>
<br>
----------------------------<br>
class MyList(list):<br>
    "I'm a subclass"<br>
<br>
l1 = []<br>
l2 = MyList()<br>
<br>
print type(l1), type(l2)<br>
print type(l1) == type(l2)<br>
print l1 == l2<br>
----------------------------<br>
<br>
when run, prints:<br>
<br>
<type 'list'> <class '__main__.MyList'><br>
False<br>
True<br>
<br>
The docs say:<br>
<br>
[<a href="http://docs.python.org/library/stdtypes.html" target="_blank">http://docs.python.org/library/stdtypes.html</a>]<br>
Objects of different types, except different numeric types and different<br>
string types, never compare equal<br>
<br>
[<a href="http://docs.python.org/release/2.7/reference/expressions.html#notin" target="_blank">http://docs.python.org/release/2.7/reference/expressions.html#notin</a>]<br>
objects of different types (emphasis)always compare unequal<br>
<br></blockquote><div><br></div><div>That definitely makes it unclear.  A little further down it says that you can customize comparison with the __cmp__ special method.  You can also override implement other specific comparison special methods, like __eq__ in your classes.  So I can definitely see what you mean.</div>
<div><br></div><div>In the case of list, a list object has a __eq__ method that it uses for determining equality, rather than using the __eq__ method of the base object type (which behaves as described in your citation).  Many of the builtin types have custom special methods for a variety of operators, including comparison.</div>
<div><br></div><div>Looking over the documentation, it seems like it could be touched up to alleviate any confusion.  Perhaps rewording to make it clear that the described behavior is the default for objects, rather than always the case.</div>
<div><br></div><div>-eric</div><div>  </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
In the test code above, l1 an l2 are different types, at least in the<br>
sense that type() returns something different for each of them.  What's<br>
the intended behavior here?  Either the code is wrong or the docs are<br>
wrong.<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><div><br></div>