list equal to subclass of list?

Roy Smith roy at panix.com
Thu May 12 08:23:04 EDT 2011


I have a vague feeling this may have been discussed a long time ago, but 
I can't find the thread, so I'll bring it up again.

I recently observed in the "checking if a list is empty" thread that a 
list and a subclass of list can compare equal:

----------------------------
class MyList(list):
    "I'm a subclass"

l1 = []
l2 = MyList()

print type(l1), type(l2)
print type(l1) == type(l2)
print l1 == l2
----------------------------

when run, prints:

<type 'list'> <class '__main__.MyList'>
False
True

The docs say:

[http://docs.python.org/library/stdtypes.html]
Objects of different types, except different numeric types and different 
string types, never compare equal

[http://docs.python.org/release/2.7/reference/expressions.html#notin]
objects of different types (emphasis)always compare unequal

In the test code above, l1 an l2 are different types, at least in the 
sense that type() returns something different for each of them.  What's 
the intended behavior here?  Either the code is wrong or the docs are 
wrong.



More information about the Python-list mailing list