[Python-ideas] True and False are singletons

Wes Turner wes.turner at gmail.com
Mon Mar 18 16:52:53 EDT 2019


'True' is a keyword. (Which is now immutable in Python 3.X?)

>>> True = 1
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy

https://docs.python.org/3/search.html?q=singleton

- "Since None is a singleton, testing for object identity (using == in C)
is sufficient. There is no PyNone_Check() function for the same reason."
  https://docs.python.org/3/c-api/none.html?highlight=singleton
- "Using a trailing comma for a singleton tuple: a, or (a,)" (?)
  https://docs.python.org/3/library/stdtypes.html?highlight=singleton#tuple

https://docs.python.org/3/library/stdtypes.html#boolean-values

https://docs.python.org/3/library/stdtypes.html#truth-value-testing


In Python 2:

>>> True
True
>>> True is True
True
>>> True = 1
>>> True is 1
True
>>> True is None
False
>>> True = None
>>> True is None
True

On Mon, Mar 18, 2019 at 7:34 AM Chris Angelico <rosuav at gmail.com> 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.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190318/75fff4a4/attachment.html>


More information about the Python-ideas mailing list