101

When storing a bool in memcached through python-memcached I noticed that it's returned as an integer. Checking the code of the library showed me that there is a place whereisinstance(val, int) is checked to flag the value as an integer.

So I tested it in the python shell and noticed the following:

>>> isinstance(True, int)True>>> issubclass(bool, int)True

But why exactly isbool a subclass ofint?

It kind of makes sense because a boolean basically is an int which can just take two values but it needs much less operations/space than an actual integer (no arithmetics, only a single bit of storage space)....

askedNov 17, 2011 at 14:43
ThiefMaster's user avatar
3
  • 12
    Here'sAlex Martelli's take on a related question.CommentedNov 17, 2011 at 14:48
  • 2
    It's worth noting that since in Python, everything is an object, with the overhead that employs, it's pretty much pointless to try to save space by makingbools smaller. If you cared about memory use, you'd be using a different language to begin with.CommentedNov 17, 2011 at 17:35
  • Also there's only one copy ofTrue andFalse in the entire process, so saving a few bytes on those two specific objects would have basically no impact on anything.CommentedJan 25, 2023 at 19:05

3 Answers3

113

From a comment onhttp://www.peterbe.com/plog/bool-is-int

It is perfectly logical, if you were around when the bool type was added to python (sometime around 2.2 or 2.3).

Prior to introduction of an actual bool type, 0 and 1 were the official representation for truth value, similar to C89. To avoid unnecessarily breaking non-ideal but working code, the new bool type needed to work just like 0 and 1. This goes beyond merely truth value, but all integral operations. No one would recommend using a boolean result in a numeric context, nor would most people recommend testing equality to determine truth value, no one wanted to find out the hard way just how much existing code is that way. Thus the decision to make True and False masquerade as 1 and 0, respectively. This is merely a historical artifact of the linguistic evolution.

Credit goes to dman13 for this nice explanation.

answeredNov 17, 2011 at 14:46
Polynomial's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this might be historically true, but idiomatically you see a lot ofsum([f(value) for value in values]) wheref(x) is some sort of filter function and you need to see how many values pass the filter.
Personally I would rather writesum(1 for value in values if f(value)), but I've actually seen respected people advocate in favor of numerical operations on bools.
I was there, and it was contentious. I saw the "value" but I disagreed and still disagree (although I was and am a Python nobody with no influence, so take that however you want). I wish they had changed it in Py3.
33

SeePEP 285 -- Adding a bool type. Relevent passage:

6) Should bool inherit from int?

=> Yes.

In an ideal world, bool might be better implemented as a separate integer type that knows how to perform mixed-mode arithmetic. However, inheriting bool from int eases the implementation enormously (in part since all C code that calls PyInt_Check() will continue to work -- this returns true for subclasses of int).

answeredNov 17, 2011 at 14:47
Steven Rumbalski's user avatar

Comments

0

Can also usehelp to check theBool's value in Console:

help(True)

help(True)Help on bool object:class bool(int) |  bool(x) -> bool |   |  Returns True when the argument x is true, False otherwise. |  The builtins True and False are the only two instances of the class bool. |  The class bool is a subclass of the class int, and cannot be subclassed. |   |  Method resolution order: |      bool |      int |      object |

help(False)

help(False)Help on bool object:class bool(int) |  bool(x) -> bool |   |  Returns True when the argument x is true, False otherwise. |  The builtins True and False are the only two instances of the class bool. |  The class bool is a subclass of the class int, and cannot be subclassed. |   |  Method resolution order: |      bool |      int |      object
answeredJan 23, 2018 at 2:40
aircraft's user avatar

1 Comment

You might want to comment out the help text so that it isn't syntax highlighted.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.