Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5249

Re: list equal to subclass of list?

Date 2011-05-12 11:29 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: list equal to subclass of list?
References <roy-B4A109.08230412052011@news.panix.com> <BANLkTik86fvQwz9bqT+wONKUmJoCrzAmjg@mail.gmail.com> <55506367-5E1C-440B-A2D4-73159339D2E6@panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.1481.1305224275.9059.python-list@python.org> (permalink)

Show all headers | View raw


Roy Smith wrote:
> On May 12, 2011, at 11:30 AM, Eric Snow wrote:
>>
>> That definitely makes it unclear.
> 
> I don't think it's unclear at all.  It's very clear.  Clearly wrong :-)

While it is wrong (it should have 'built-in' precede the word 'types'), 
it is not wrong in the way you think -- a subclass *is* a type of its 
superclass.


>> A little further down it says that you can customize comparison with 
>> the __cmp__ special method. 
 >
> I read that as saying that if you implement __eq__(), you must make sure 
> that it returns False if self and other have different types (and 
> likewise, __ne__() should return True for that case).

Your understanding is flawed.  If your object does not know how to 
compare itself to some other object, it should return NotImplemented -- 
at that point Python will follow the rules outlined in the docs.  By 
returning NotImplemented you are allowing the other object a chance to 
perform the comparison -- after all, it might know how!  :)  If the 
other object also returns NotImplemented then (drum-roll please) they 
won't compare equal.


> The same way that it says that obj1.__lt__(obj2) must return a consistent
 > result for all types of obj1 and obj2.

Where do you see that?  I couldn't find it.

The point of being able to write your own rich comparison methods is so 
you can control what happens -- there is no "must" about it.  This is 
Python -- do what you want!  :)

~Ethan~

PS
I have a broken sense of humor -- sometimes it works, sometimes it 
doesn't.  My apologies in advance if my attempt at humor was not funny.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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