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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:11:38+00:00 2026-06-12T05:11:38+00:00

What is the specific code, in order, being executed when I ask for something

  • 0

What is the specific code, in order, being executed when I ask for something like

>>> 1 <= 3 >= 2
True

If both have equal precedence and it’s just the order of their evaluation, why does the second inequality function as (3 >= 2) instead of (True >= 2)

Consider for example the difference between these

>>> (1 < 3) < 2
True

>>> 1 < 3 < 2
False

Is it just a pure syntactical short-cut hard-coded into Python to expand the second as the and of the two statements?

Could I change this behavior for a class, such that a <= b <= c gets expanded to something different? It’s looking like the following is the case

a (logical operator) b (logical operator) c 
    --> (a logical operator b) and (b logical operator c)

but the real question is how this gets implemented in code.

I’m curious so that I can replicate this kind of __lt__ and __gt__ behavior in some of my own classes, but I am confused about how this is accomplished holding the middle argument constant.

Here’s a specific example:

>>> import numpy as np

>>> tst = np.asarray([1,2,3,4,5,6])

>>> 3 <= tst
array([False, False,  True,  True,  True,  True], dtype=bool)

>>> 3 <= tst <= 5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/ely/<ipython-input-135-ac909818f2b1> in <module>()
----> 1 3 <= tst <= 5

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

It would be nice to override this so that it “just works” with arrays too, like this:

>>> np.logical_and(3 <= tst, tst <= 5)
array([False, False,  True,  True,  True,  False], dtype=bool)

Added for clarification

In the comments it is indicated that I did a poor job of explaining the question. Here’s some clarifying remarks:

1) I am not looking for a simple explanation of the fact that the interpreter pops an and in between the two chained inequalities. I already knew that and said so above.

2) For an analogy to what I want to do, consider the with statement (link). The following:

with MyClass(some_obj) as foo:
    do_stuff()

unpacks into

foo = MyClass(some_obj)
foo.__enter__()
try:
    do_stuff()
finally:
    foo.__exit__()

So by writing MyClass appropriately, I can do many special things inside of the with statement.

I am asking whether there is a similar code unpacking of the chained inequality by which I can intercept what it’s doing and redirect it to use array-style logical operators instead just for the classes I care about.

I feel this is very clear from my question, especially the example, but hopefully this makes it more clear.

  • 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-12T05:11:40+00:00Added an answer on June 12, 2026 at 5:11 am

    I’m not totally sure what you’re looking for, but a quick disassembly shows that a < b < c is not compiled to the same bytecode as a < b and b < c

    >>> import dis
    >>>
    >>> def f(a, b, c):
    ...     return a < b < c
    ...
    >>> dis.dis(f)
      2           0 LOAD_FAST                0 (a)
                  3 LOAD_FAST                1 (b)
                  6 DUP_TOP
                  7 ROT_THREE
                  8 COMPARE_OP               0 (<)
                 11 JUMP_IF_FALSE_OR_POP    21
                 14 LOAD_FAST                2 (c)
                 17 COMPARE_OP               0 (<)
                 20 RETURN_VALUE
            >>   21 ROT_TWO
                 22 POP_TOP
                 23 RETURN_VALUE
    >>>
    >>> def f(a, b, c):
    ...     return a < b and b < c
    ...
    >>> dis.dis(f)
      2           0 LOAD_FAST                0 (a)
                  3 LOAD_FAST                1 (b)
                  6 COMPARE_OP               0 (<)
                  9 JUMP_IF_FALSE_OR_POP    21
                 12 LOAD_FAST                1 (b)
                 15 LOAD_FAST                2 (c)
                 18 COMPARE_OP               0 (<)
            >>   21 RETURN_VALUE
    

    Edit 1: Digging further, I think this is something weird or wrong with numpy. Consider this example code, I think it works as you would expect.

    class Object(object):
        def __init__(self, values):
            self.values = values
        def __lt__(self, other):
            return [x < other for x in self.values]
        def __gt__(self, other):
            return [x > other for x in self.values]
    
    x = Object([1, 2, 3])
    print x < 5 # [True, True, True]
    print x > 5 # [False, False, False]
    print 0 < x < 5 # [True, True, True]
    

    Edit 2: Actually this doesn’t work “properly”…

    print 1 < x # [False, True, True]
    print x < 3 # [True, True, False]
    print 1 < x < 3 # [True, True, False]
    

    I think it’s comparing boolean values to numbers in the second comparison of 1 < x < 3.

    Edit 3: I don’t like the idea of returning non-boolean values from the gt, lt, gte, lte special methods, but it’s actually not restricted according to the Python documentation.

    http://docs.python.org/reference/datamodel.html#object.lt

    By convention, False and True are returned for a successful
    comparison. However, these methods can return any value…

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

Sidebar

Related Questions

I have this code to move my uploaded file to a specific directory: if
Okay, I actually don't have code as of yet because i'm just picking out
I have to sort my Array array with specific key likecount with descending order.
My question is not about a specific code snippet but more general, so please
Not sure if this is against stackoverflow rules as it's not a specific code
I've got a function to do ANOVA for a specific column (this code is
Here is ALL my code for this specific view controller. As the title of
A specific example: becoming familiar with django's project source code (core, contrib, utils, etc.).
When I run the code for this specific value of dt, an exception is
I used this code to access the Specific XML under the SharePoint Mapped Folder;

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.