[Tutor] line class

Kent Johnson kent37 at tds.net
Wed Jul 9 03:37:04 CEST 2008


On Tue, Jul 8, 2008 at 6:29 PM, Christopher Spears
<cspears2002 at yahoo.com> wrote:
> I have been reading everyone's comments on my line class.  I have decided to implement some of the suggestions.  Someone suggested that I create a Point.__cmp__ method.  Here is what I have so far:
>
> def __cmp__(self, other):
>        if self.x == other.x and self.y == other.y:
>            return 0
>        elif self.x < other.x and self.y < other.y:
>            return -1
>        elif self.x > other.x and self.y > other.y:
>            return 1
>
> Figuring out the results for the above situations was easy.  However, what should I do with the following situations:
> self.x > other.x and self.y < other.y
> self.x < other.x and self.y > other.y

It might be easier to define __eq__ and __ne__ instead of __cmp__ if
you just want to test for equality. Otherwise you can pick an
arbitrary ordering or see if there is something that makes sense to
the rest of your application.

Kent


More information about the Tutor mailing list