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

The Archive Base Latest Questions

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

Python supports chained comparisons : 1 < 2 < 3 translates to (1 <

  • 0

Python supports chained comparisons: 1 < 2 < 3 translates to (1 < 2) and (2 < 3).

I am trying to make an SQL query using SQLAlchemy which looks like this:

results = session.query(Couple).filter(10 < Couple.NumOfResults < 20).all()

The results I got were not as expected. I’ve turned the engine’s echo=True keyword, and indeed – the generated SQL query only included one of the two comparisons.

I can’t find any documentation that explicitly says this is forbidden. I assumed that if this type of expression is supported in Python, it should be supported in SQLAlchemy as well.

Why doesn’t this work? I have one possible solution in mind (shared in answers), but will be glad to hear other opinions.

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

    The reason is that Python actually evaluates something akin to this:

    _tmp = Couple.NumOfResults
    (10 < _tmp and _tmp < 20)
    

    The and operator is unsupported in SQLAlchemy (one should use and_ instead). And thus – chained comparisons are not allowed in SQLAlchemy.

    In the original example, one should write this code instead:

    results = session.query(Couple).filter(and_(10 < Couple.NumOfResults, 
                                                Couple.NumOfResults < 20)).all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ubuntu 10.04, which supports Python up to version 2.6.5. However, I
I'm using the python dateutil module for a calendaring application which supports repeating events.
I'm trying to make a small game that supports Python scripting. I've no problems
Apparently Python only supports 2 minor versions (like 2.X), so that would mean Python
I'm trying to find a decent IDE that supports Python 3.x, and offers code
I have a module which supports creation of geographic objects using a company-standard interface.
is there an activerecord (any similar SQL-wrapper) for python? which is good for: used
do you know if Python supports some keyword or expression like in C++ to
Is there a structure in Python which supports similar operations to C++ STL map
Python has heapq module which implements heap data structure and it supports some basic

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.