Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6853689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:32:30+00:00 2026-05-27T01:32:30+00:00

The following piece of code class point: def __init__(self, x, y): self.x = x

  • 0

The following piece of code

class point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def dispc(self):
        return ('(' + str(self.x) + ',' + str(self.y) + ')')

    def __cmp__(self, other):
        return ((self.x > other.x) and (self.y > other.y))

works fine in Python 2, but in Python 3 I get an error:

>>> p=point(2,3)
>>> q=point(3,4)
>>> p>q
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: point() > point()

It only works for == and !=.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:32:31+00:00Added an answer on May 27, 2026 at 1:32 am

    You need to provide the rich comparison methods for ordering in Python 3, which are __lt__, __gt__, __le__, __ge__, __eq__, and __ne__. See also: PEP 207 — Rich Comparisons.

    __cmp__ is no longer used.


    More specifically, __lt__ takes self and other as arguments, and needs to return whether self is less than other. For example:

    class Point(object):
        ...
        def __lt__(self, other):
            return ((self.x < other.x) and (self.y < other.y))
    

    (This isn’t a sensible comparison implementation, but it’s hard to tell what you were going for.)

    So if you have the following situation:

    p1 = Point(1, 2)
    p2 = Point(3, 4)
    
    p1 < p2
    

    This will be equivalent to:

    p1.__lt__(p2)
    

    which would return True.

    __eq__ would return True if the points are equal and False otherwise. The other methods work analogously.


    If you use the functools.total_ordering decorator, you only need to implement e.g. the __lt__ and __eq__ methods:

    from functools import total_ordering
    
    @total_ordering
    class Point(object):
        def __lt__(self, other):
            ...
    
        def __eq__(self, other):
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following piece of code: class Foo { public Foo() { Bar
I have the following piece of code in Visual C++ 2005 : : class
I have the following piece of code in my DataModel.cs class: public User ValidateUser(string
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
The following piece of code runs fine when parallelized to 4-5 threads, but starts
Let's say we have the following piece of code: public class Event { }
I have the following piece of code: class ICookable { public: virtual void CookMe
I have the following piece of code: class Student { public: Student(){} void display()
following piece of code: class a{ public function __get($key) { if($key == 'obj') {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.