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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:34:32+00:00 2026-05-31T00:34:32+00:00

Possible Duplicate: What's the best way to implement an 'enum' in Python? I’m writing

  • 0

Possible Duplicate:
What's the best way to implement an 'enum' in Python?

I’m writing a function that, ideally, I’d like to return one of three states: “yes”, “no”, and “don’t know”.

  1. Do any programming languages have a type that has three, and only three states? Like a boolean, but with three states instead of two?

  2. In languages that don’t have such a type (like Python), what’s the best type to represent this?

    Currently I think I’ll go with an integer (0 for “no”, 1 for “don’t know” and 2 for “yes”), but maybe there’s a better way? Integers seem a bit “magic number”.

    I could return True, False or None, but as None would evaluate to False in most comparison contexts it seems a bit ripe for errors.

  • 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-31T00:34:34+00:00Added an answer on May 31, 2026 at 12:34 am

    In Python I’d do that with a wrapper object that holds one of those three values; I’d use True, False, and None. Since the implicit truthiness value of a Boolean-like object with three possible values is problematic, we’ll solve that by disallowing that entirely (raising an exception in __nonzero__(), or in Python 3, __bool__()), thus requiring that comparisons always be done explicitly, using in, ==, or !=. We’ll implement equality as identity so that only the specific singleton values True, False, and None are matched.

    class Tristate(object):
    
        def __init__(self, value=None):
           if any(value is v for v in (True, False, None)):
              self.value = value
           else:
               raise ValueError("Tristate value must be True, False, or None")
    
        def __eq__(self, other):
           return (self.value is other.value if isinstance(other, Tristate)
                   else self.value is other)
    
        def __ne__(self, other):
           return not self == other
    
        def __nonzero__(self):   # Python 3: __bool__()
           raise TypeError("Tristate object may not be used as a Boolean")
    
        def __str__(self):
            return str(self.value)
    
        def __repr__(self):
            return "Tristate(%s)" % self.value
    

    Usage:

    t = Tristate(True)
    t == True           # True
    t != False          # True
    t in (True, False)  # True
    bool(t)             # Exception!
    if t: print "woo"   # Exception!
    

    When using Tristate objects, you must explicitly specify which values to match, i.e. foo == True or bar != None. You can also do foo in (False, None) to match multiple values (though of course in two values is the same as != with a single value). If there are other logic operations you wish to be able to perform with these objects, you could implement these as methods, or possibly by overriding certain operators (sadly, however, logical not, and, and or are not overrideable, though there’s a proposal to add that).

    Also note that you can’t override id() in Python, so e.g. Tristate(None) is None is False; the two objects are in fact different. Since good Python style is to use is when comparing against singletons, this is unfortunate, but unavoidable.

    Edit 4/27/16: Added support for comparing one Tristate object to another.

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

Sidebar

Related Questions

Possible Duplicate: What’s the best way to implement an ‘enum’ in Python? What is
Possible Duplicate: What's the best way to develop a sideswipe menu like the one
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What is the best way to remove accents in a python unicode
Possible Duplicate: Best Way to Sprite Images? I have the following image that I
Possible Duplicate: What is the best way to return XML from a controller's action
Possible Duplicate: Best css reset What is the best way to implement CSS reset?
Possible Duplicate: Best way to stop SQL Injection in PHP I would like to
Possible Duplicate: Best way to stop SQL Injection in PHP I'd like a simple
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating 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.