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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:33:20+00:00 2026-06-11T04:33:20+00:00

I have the following C program as an example of what I would like

  • 0

I have the following C program as an example of what I would like to be able to do in python:

foo@foo:~/$ cat test.c 
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool get_false(){
    return false;
}

bool get_true(){
    return true;
}

void main(int argc, char* argv[]){

    bool x, y;

    if ( x = get_false() ){
        printf("Whiskey. Tango. Foxtrot.\n");
    }   

    if ( y = get_true() ){
        printf("Nothing to see here, keep moving.\n");
    }   
}
foo@foo:~/$ gcc test.c -o test
test.c: In function ‘main’:
test.c:13: warning: return type of ‘main’ is not ‘int’
foo@foo:~/$ ./test 
Nothing to see here, keep moving.
foo@foo:~/$ 

In python, the only way I know how to do this is:

foo@foo:~/$ cat test.py
def get_false():
    return False

def get_true():
    return True

if __name__ == '__main__':
    x = get_false()
    if x:
        print "Whiskey. Tango. Foxtrot."

    y = get_true()
    if y:
        print "Nothing to see here, keep moving."

    #if (z = get_false()):
    #    print "Uncommenting this will give me a syntax error."

    #if (a = get_false()) == False:
    #    print "This doesn't work either...also invalid syntax."
foo@foo:~/$ python test.py
Nothing to see here, keep moving.

Why? Because I’d like to be able to say:

if not (x=get_false()): x={}

Basically I’m working around a bad API where the type returned is either a dict when data is available, or False. Yes, a valid answer would be to return consistent types and to use Exceptions instead of False for a failure mode indicator. I can’t change the underlying API though, and I run into this pattern quite a bit in environments like Python with dynamic typing (read: without strict typing for function/method interfaces).

Any suggestions on how to reduce the if/else overhead?

  • 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-11T04:33:22+00:00Added an answer on June 11, 2026 at 4:33 am

    You can use

    x = get_false() or {}
    

    Should get_false() return a False value, Python will return the second operand of or.

    See section 5.10 of the Python reference manual. (It’s been there since at least Python 2.0).

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

Sidebar

Related Questions

I have a PyQt program used to visualize some python objects. I would like
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following c program which launches a Gtk Program on ubuntu: #include
My intended program flow would look like the following if it were possible: typedef
I would like to do the following (just an example, the real code has
I would like to do the following . I have declared a structure in
If I write a program like the following one, g++ and visual studio have
I have problem with the tar linux program. I would like to create a
I have the following program. I wonder why it outputs -4 on the following
I have the following program flow: User fills in form My app generates a

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.