3

I know that Python guarantees that there is only one instance of NoneType, the None object, so that you can safely use is None to test if something equals None.

Is there an equivalent guarantee for bool True and False (i.e. that there is only one instance of each)?

If not, why not?

EDIT: In particular, I've noticed that (n+0) is (0+n) gives True for n in range(-5, 257) and False otherwise. In other words, zero, the first 256 positive and the first 5 negative integers seem to be pre-cached and are not instanced again. I am guessing that that's a choice of the interpreter (CPython, in my case) and not a specification of the language. And bool derives from int, so I still have to wonder about what expectations I can have with other interpreters.

EDIT: To clarify, since this seems to have generated a lot of confusion, my intention is not to test the boolean interpretation of a value. For that I would never use is True or is False. My intention is to be able to tell apart False from everything else, in a variable that can have values of several types including empty strings, zeros, and None, and similarly for True. I'm myself an experienced programmer, of the kind who cringes when I see "if booleanvar == True".

NOTE ON DUPLICATES: The questions this one was alleged to be a duplicate of (this and this) don't answer this question; they merely state that bool is a subclass of int that differ mainly in their repr, not if True and False are guaranteed to be unique.

Also, note that it's not a question about what the names True and False are bound to, but about the instances of the class bool.

Community
  • 1
  • 1
Pedro Gimeno
  • 2,837
  • 1
  • 25
  • 33
  • 1
    Why would you need/want to do this? Something like `if var is True:` is ugly compared to just `if var:`. –  Jul 19 '14 at 17:06
  • 2
    Generally speaking: Don't test against `True` or `False`. Use the implicit boolean values of expressions instead. – Martijn Pieters Jul 19 '14 at 17:08
  • 4
    1. It depends which version of Python; and 2. Never rely on this (per [PEP-0008](http://legacy.python.org/dev/peps/pep-0008/#programming-recommendations): *"Yes: `if greeting:`, No: `if greeting == True:`, Worse: `if greeting is True:`*). – jonrsharpe Jul 19 '14 at 17:08
  • PEP-0008 applies to values known to be boolean. I want to use it to test for type and value at the same time. My question is if there's a guarantee that these objects are unique; I'm not looking for advice on the usage. – Pedro Gimeno Jul 19 '14 at 17:11
  • @MartijnPieters: The linked question claimed as "duplicate" does not answer this. I'm not asking what the True and False names are bound to; I'm asking if the object instances that True and False are initially bound to are guaranteed to be unique. I know that there can be multiple instances of the string 'x', for example, all with the same value, and thus the `is` operator may fail. I'll edit the question to remove True and False from the tests. – Pedro Gimeno Jul 19 '14 at 17:23
  • @PedroGimeno: that question *very much* answers that. In Python 2, no, they are not guaranteed to be unique because you can rebind the names. In Python 3, they are unique values. – Martijn Pieters Jul 19 '14 at 17:25
  • 2
    @PedroGimeno: I strongly urge you not to use `is True` or `is False` however. You'd use `if something:` instead, and **not** test against True or False. If you have to have a boolean, at best use `isinstance(object, bool)`. – Martijn Pieters Jul 19 '14 at 17:26
  • 1
    The *built-in* names `True` and `False` are singletons, however. – Martijn Pieters Jul 19 '14 at 17:27

2 Answers2

8

From the docs (https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy):

Booleans

These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects.

There are only two objects, any computation producing a boolean will produce one of those two existing objects:

>>> (1 == 1) is True
True
>>> (1 == 0) is False
True
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

The bool type has only two instances, True and False. Furthermore, it can't be subclassed, so there's no way to create a derived class that can have additional instances.

But even though it's guaranteed, there's seldom a good reason to rely upon it. You should usually use if x rather than if x is True, and avoid situations where you need to distinguish True from other truthy values or False from other falsy values.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • But sometimes you generate an instance by performing calculations; e.g. `(917+543) is (916+544)` gives `False`. So do I have a guarantee that if I produce False or True, I won't have a new instance? It's not clear from your answer. – Pedro Gimeno Jul 20 '14 at 01:13
  • @PedroGimeno you're comparing two ints and asking if they are the same references. Nothing to do with booleans in your question. If you call bool on those ints though, you get the result you expect. ```>>> bool(917+543) is bool(916+544)``` ```True``` – Josh Smeaton Jul 20 '14 at 01:46
  • @JoshSmeaton: My question is about instances. With `is`, I'm asking if they are the same instance. I used the `int` example because `bool` is a subclass of `int`. Please see the recent edit to my question. – Pedro Gimeno Jul 20 '14 at 01:53