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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:03:55+00:00 2026-05-31T18:03:55+00:00

If statements in Python allow you to do something like: if not x: print

  • 0

If statements in Python allow you to do something like:

   if not x:
       print "X is false."

This works if you’re using an empty list, an empty dictionary, None, 0, etc, but what if you have your own custom class? Can you assign a false value for that class so that in the same style of conditional, it will return false?

  • 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-31T18:03:57+00:00Added an answer on May 31, 2026 at 6:03 pm

    Since Python 3, you need to implement the __bool__ method on your class. This should return True or False to determine the truth value:

    class MyClass:
        def __init__(self, val):
            self.val = val
        def __bool__(self):
            return self.val != 0  #This is an example, you can use any condition
    
    x = MyClass(0)
    if not x:
        print('x is false')
    

    If __bool__ has not been defined, the implementation will call __len__ and the instance will be considered True if it returned a nonzero value. If __len__ hasn’t been defined either, all instances will be considered True.

    For further reading:

    • https://docs.python.org/3/library/stdtypes.html#truth-value-testing

    In Python 2, the special method __nonzero__ was used instead of __bool__.

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

Sidebar

Related Questions

i have written this python program. whenever i run the script using parameters like
In Python, the following statements do not work: f = open("ftmp", "rw") print >>
I'm doing print statements in python. I'm executing my script like so: python script.py
When using IF statements in Python, you have to do the following to make
in the python console the following statement works perfectly fine (i guess using eval
Can we initialize Python objects with statement like this: a = b = c
I am using Python 3.0 to write a program. In this program I deal
Python does not warrant the use of semicolons to end statements. So why is
I'd like to extend the Python interactive shell to allow execution of non-python specific
Or statements in python do not seem to work as in other languages since:

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.