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 7778521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:25:08+00:00 2026-06-01T18:25:08+00:00

In the manual is says: in general, __lt__() and __eq__() are sufficient, if you

  • 0

In the manual is says:

in general, __lt__() and __eq__() are sufficient, if you want the
conventional meanings of the comparison operators

But I see the error:

>       assert 2 < three
E       TypeError: unorderable types: int() < IntVar()

when I run this test:

from unittest import TestCase

class IntVar(object):

    def __init__(self, value=None):
        if value is not None: value = int(value)
        self.value = value

    def __int__(self):
        return self.value

    def __lt__(self, other):
        return self.value < other

    def __eq__(self, other):
        return self.value == other

    def __hash__(self):
        return hash(self.value)

class DynamicTest(TestCase):

    def test_lt(self):
        three = IntVar(3)
        assert three < 4
        assert 2 < three
        assert 3 == three

I am surprised that when IntVar() is on the right, __int__() is not being called. What am I doing wrong?

Adding __gt__() fixes this, but means I don’t understand what the minimal requirements are for ordering…

Thanks,
Andrew

  • 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-06-01T18:25:09+00:00Added an answer on June 1, 2026 at 6:25 pm

    Python 3.x will never do any type coercions for operators, so __int__() is not used in this context. The comparison

    a < b
    

    will first try to call type(a).__lt__(a, b), and if this returns NotImplemented, it will call type(b).__gt__(b, a).

    The quote from the documentation is about making comparisons work for a single type, and the above explanation shows why this would be enough for a single type.

    To make your type interact correctly with int, you should either implement all the comparison operator, or use the total_ordering decorator available in Python 2.7 or 3.2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perforce manual says that if you want to automatically expand RCS keywords in file
This is probably common sense but just want to make sure. As the manual
I have read what the RSpec manual says about the difference, but some things
The Zend Framework Manual says the following: 60.3.1. Escaping Output One of the most
The manual does not answer this question : it says the name of the
The php.net page here http://www.php.net/manual/en/mysqlinfo.library.choosing.php says this Configure commands for using mysqlnd // Recommended,
http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS6.html In there it says For the photo library, the existing interface supports the
The GCC manual says: -fobjc-direct-dispatch Allow fast jumps to the message dispatcher. On Darwin
I just read about unset variable through php manual. The php manual says unset()
I am using liferay portal and displaying graphs on portlet... The manual says the

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.