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

  • Home
  • SEARCH
  • 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 810455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:53:17+00:00 2026-05-15T00:53:17+00:00

So I have a one liner: import decimal; h = decimal.Decimal(‘100.0’); (h > .01,

  • 0

So I have a one liner:

import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01))

All it does is make a Decimal object holding 100.0, and compares it to .01 (the float) in various ways.

My result is:

>>> import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01))
(False, True, NotImplemented, NotImplemented)

From the docs: “A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments.”

So really there are three questions here.

  1. When a rich comparison method returns NotImplemented, what happens? Why doesn’t it raise an Exception?

  2. When it gets NotImplemented, why does it return False in the first case, and True in the second? bool(NotImplemented) should be a constant.

  3. Does it simply fall back to id() checking? It seems no (or yes, but backwards):

(ignore this line, formatting is screwed up and this fixes it)

from decimal import Decimal
h = Decimal('100.0')
f = .01
print h < f, id(h) < id(f)
print h > f, id(h) > id(f)

My results were tested on:

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32

Edit: Documentation about ordering: http://docs.python.org/library/stdtypes.html#comparisons

  • 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-15T00:53:18+00:00Added an answer on May 15, 2026 at 12:53 am

    When a rich comparison method returns
    NotImplemented, what happens? Why
    doesn’t it raise an Exception?

    it delegates to the converse method (e.g., __lt__ when the operator is >) RHS in the comparison (the float) — which in this case also returns NotImplemented — and finally falls back to Python 2’s silly old rules for heterogeneous comparisons.

    When it gets NotImplemented, why does
    it return False in the first case, and
    True in the second?
    bool(NotImplemented) should be a
    constant.

    No bool involved — since both sides of the comparison return NotImplemented (due to a deliberate design decision to NOT support any operation between decimals and floats), the silly old rules are used as a fallback (and in a recent-enough version will be comparing the types, not the instances — id has nothing to do with it, therefore). In Python 3, such an unsupported heterogeneous comparison would fail, and raise a clear exception, but in Python 2, for backwards compatibility, that just can’t happen — it must keep behaving in the silly way it’s behaved throughout Python 2’s lifetime.

    Introducing backwards incompatibilities to fix what are now considered design errors, like this part about het comparisons, was the core reason to introduce Python 3. As long as you’re sticking to Python 2 (e.g. because it has more third party extensions, etc), you need to grin and bear with these imperfections that are fixed in Python 3 only.

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

Sidebar

Related Questions

No related questions found

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.