3

Does each variable which gets assigned a True or False (e.g: variable = True), reference two constant, unchanging addresses in memory which are reserved for holding respectively True and False?

In context: I was researching the difference between == and is in this thread, from which (according to my understanding) i learned that == compares the values of two objects, and is checks whether or not two objects are one and the same (or rather, whether they occupy the same address in memory).

Then i tried out the following code:

foo = False


if foo is False:
    print("1")

I was expecting for an error to occur, but it didn't (thinking that 'False' would be treated as a value, and is not being able to deal with a value). Instead it passed.

So is my guess correct? Does every boolean variable in fact constantly reference two objects in memory with an unchanging address (True and False)?

Magikarp
  • 31
  • 2
  • 2
    `True` and `False` are _singleton_ objects, if that's what you mean. There is only one actual occurrence of `True` in a program, and all the mentions of `True` just _refer_ to it. – John Gordon Aug 22 '23 at 00:13
  • I don't know if the memory address is actually fixed or not. – John Gordon Aug 22 '23 at 00:14
  • 6
    *EVERYTHING* in Python is an object in memory. But that doesn't mean that using tests like `foo is False` is a good idea, because that destroys Python's ability to treat *anything* as a boolean value (basically, numeric zeros and empty containers are "falsy", most everything else is "truthy"). The proper way to test for a false value is `if not foo:`. – jasonharper Aug 22 '23 at 00:16
  • @jasonharper If a boolean value is an object in memory, what is the difference between 'if x is True', and 'if x'? Don't they both simply check whether x refers to an address containing the True object? Why is the latter preferred? Is it convention or is there another reason? – Magikarp Aug 22 '23 at 00:24
  • He told you the reason. `if x is True:` means it will not work with other things that can be used to designate falsity, like 0 or the empty string or list. Saying `if x:` reads better and is more flexible. – Tim Roberts Aug 22 '23 at 00:25
  • `1` is a truthy object, but `1 is True` is absolutely not true, because those are two different objects. – jasonharper Aug 22 '23 at 00:28
  • @Tim_Roberts Right. Trying to wrap my head around this. So 'if x is True' will only pass when referencing True. 'If x' in addition also passes when referencing objects containing truthy values (non-null). Since the latter encapsulates a wider definition of what is truthy, it is preferred? – Magikarp Aug 22 '23 at 00:34
  • @Magikarp that is `x is True` is true if an only if `x` is *the singleton object `False`*. However, `x` is true for many, many other objects – juanpa.arrivillaga Aug 22 '23 at 00:36
  • @Magikarp Truthy does not mean "non null". – juanpa.arrivillaga Aug 22 '23 at 00:36
  • As an aside, not sure why you expected an error to occur. Conisder, `foo = "foobar"`, then `if x is False` doesn't raise an error (the condition is just not true, and the if block isn't executed). Also, all objects in CPython have a fixed memory address (this may not be true if certain types of garbage collection were used in CPython, but it is not the case). But if you mean "does `True` and `False` always evaluate to the same object", then that is true. `True` and `False` (and `None`) are all language guaranteed singletons – juanpa.arrivillaga Aug 22 '23 at 00:38
  • @juanpa.arrivillaga The reason i expected it is because i'm new at Python, and this just happened to be a rabbithole i chose to go down in :) Somebody else mentioned the concept of singleton objects (which i am not familiar with), and i might take a gander at it later on. But thanks for the elucidation. – Magikarp Aug 22 '23 at 00:48
  • @Magikarp it just means "there is only one". – juanpa.arrivillaga Aug 22 '23 at 00:49

0 Answers0