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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:30:31+00:00 2026-05-10T23:30:31+00:00

I just came across this idiom in some open-source Python, and I choked on

  • 0

I just came across this idiom in some open-source Python, and I choked on my drink.

Rather than:

if isUp:     return 'Up' else:     return 'Down' 

or even:

return 'Up' if isUp else 'Down' 

the code read:

return isUp and 'Up' or 'Down' 

I can see this is the same result, but is this a typical idiom in Python? If so, is it some performance hack that runs fast? Or is it just a once-off that needs a code review?

  • 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. 2026-05-10T23:30:32+00:00Added an answer on May 10, 2026 at 11:30 pm

    The ‘a and b or c’ idiom was the canonical way to express the ternary arithmetic if in Python, before PEP 308 was written and implemented. This idiom fails the ‘b’ answer is false itself; to support the general case, you could write

     return (a and [b] or [c])[0] 

    An alternative way of spelling it was

     return (b,c)[not a] 

    which, with the introduction of the bool type, could be rewritten as

     return (c,b)[bool(a)] 

    (in case it isn’t clear: the conversion to bool, and the not operator, is necessary if a is not known to be bool already)

    Today, the conditional expression syntax should be used if the thing must be an expression; else I recommend to use the if statement.

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

Sidebar

Ask A Question

Stats

  • Questions 133k
  • Answers 133k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, you can do it with side effects easily enough,… May 12, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer QD was deprecated because there is no way to do… May 12, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer Since Java 6, the JDK contains a simple HTTP server… May 12, 2026 at 6:34 am

Related Questions

I just came across this code and a few Google searches turn up no
I just came across this question about initializing local variables. Many of the answers
I want to use WebKit as the layout/rendering engine, and I want to code
Just came across this website . Feature 9 is memory management and they claim

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.