Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5246
| Date | 2011-05-12 09:43 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: list equal to subclass of list? |
| References | <roy-B4A109.08230412052011@news.panix.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1479.1305217887.9059.python-list@python.org> (permalink) |
Roy Smith wrote: > 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 This part of the documentation is talking about built-in types, which your MyList is not. > [http://docs.python.org/release/2.7/reference/expressions.html#notin] > objects of different types *always* compare unequal Should probably have the word 'built-in' precede 'types' here, since constructed objects can do whatever they have been told to do. > 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. --> MyList.__mro__ (<class '__main__.MyList'>, <type 'list'>, <type 'object'>) MyList is a list -- just a more specific kind of list -- as can be seen from its mro; this is analogous to a square (2 sets of parallel lines joined at 90 degree angles, both sets being the same length) also being a rectangle (2 sets of parallel lines joined at 90 degree angles). > What's the intended behavior here? Either the code is wrong or the docs > are wrong. The code is correct. ~Ethan~ PS Yes, I know my square/rectangle definitions are incomplete, thanks. ;)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
list equal to subclass of list? Roy Smith <roy@panix.com> - 2011-05-12 08:23 -0400
Re: list equal to subclass of list? Ethan Furman <ethan@stoneleaf.us> - 2011-05-12 09:43 -0700
Re: list equal to subclass of list? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-13 00:18 +0000
Re: list equal to subclass of list? Ethan Furman <ethan@stoneleaf.us> - 2011-05-12 18:04 -0700
Re: list equal to subclass of list? Roy Smith <roy@panix.com> - 2011-05-12 21:53 -0400
Re: list equal to subclass of list? Ethan Furman <ethan@stoneleaf.us> - 2011-05-12 11:29 -0700
Re: list equal to subclass of list? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-05-12 22:40 +0200
Re: list equal to subclass of list? Roy Smith <roy@panix.com> - 2011-05-12 13:49 -0700
Re: list equal to subclass of list? Ethan Furman <ethan@stoneleaf.us> - 2011-05-12 14:48 -0700
csiph-web