[Python-ideas] True and False are singletons
Richard at Damon-Family.org
Mon Mar 18 08:17:35 EDT 2019
On 3/18/19 7:32 AM, Chris Angelico wrote:
> On Mon, Mar 18, 2019 at 10:14 PM Juancarlo Añez <apalala at gmail.com> wrote:
>> It came to my attention that:
>> In the original PEP True and False are said to be singletons https://www.python.org/dev/peps/pep-0285/, but it's not in the Data Model https://docs.python.org/3/reference/datamodel.html
>> This came to my attention by code wanting to own the valid values in a dict's key:
>> if settings[MY_KEY] is True:
>> If True and False are singletons in the spec (and not only in the CPython implementation), it should be prominent and well known.
> "Singleton" technically means that there is only one such object.
> 'None' is a singleton, by language specification; if type(x) is
> type(None), you can safely assume that x is None. Booleans are a bit
> more tricky; there will only ever be those two, but they're two. IMO
> the PEP is minorly inaccurate to use the word "singleton" there, but
> it's no big deal. As Remi says, the two built-in ones are the only two
> instances of that type.
Which says that the type of True or False isn't a singleton, but those
particular values are. There may be other objects with values that are
Truthy or Falsey, but if the value actually IS True or False, the object
WILL be those particular objects.
As a comparison, if the Tuple (1,2) was described as a Singleton, then
any computation that generates that value would all need to return the
exact same object, but they don't (the language could make that a true
statement). When converting a Truthy or Falsey value to True or False,
Python will ALWAYS grab those particular objects, and not create a new
object with the same value, so they are Singletons, even if the type
Yes, in many languages the term Singleton is used for Types and not
values, and in part that difference is due to that fact that in Python
ALL values are objects, and names are just bound to some object, so the
idea of Singleton primitive values actually makes sense.
More information about the Python-ideas