Add the 'bool' type and its values 'False' and 'True', as described in
PEP 285. Everything described in the PEP is here, and there is even
some documentation. I had to fix 12 unit tests; all but one of these
were printing Boolean outcomes that changed from 0/1 to False/True.
(The exception is test_unicode.py, which did a type(x) == type(y)
style comparison. I could've fixed that with a single line using
issubtype(x, type(y)), but instead chose to be explicit about those
places where a bool is expected.
Still to do: perhaps more documentation; change standard library
modules to return False/True from predicates.
diff --git a/Doc/lib/libfuncs.tex b/Doc/lib/libfuncs.tex
index a2407d7..814b26b 100644
--- a/Doc/lib/libfuncs.tex
+++ b/Doc/lib/libfuncs.tex
@@ -77,6 +77,16 @@
to \code{\var{function}(*\var{args}, **\var{keywords})}.
\end{funcdesc}
+\begin{funcdesc}{bool}{x}
+ Convert a value to a Boolean, using the standard truth testing
+ procedure. If \code{x} is false, this returns \code{False};
+ otherwise it returns \code{True}. \code{bool} is also a class,
+ which is a subclass of \code{int}. Class \code{bool} cannot be
+ subclassed further. Its only instances are \code{False} and
+ \code{True}.
+\indexii{Boolean}{type}
+\end{funcdesc}
+
\begin{funcdesc}{buffer}{object\optional{, offset\optional{, size}}}
The \var{object} argument must be an object that supports the buffer
call interface (such as strings, arrays, and buffers). A new buffer
diff --git a/Doc/lib/libstdtypes.tex b/Doc/lib/libstdtypes.tex
index 2bd6420..d63e4d5 100644
--- a/Doc/lib/libstdtypes.tex
+++ b/Doc/lib/libstdtypes.tex
@@ -2,10 +2,8 @@
The following sections describe the standard types that are built into
the interpreter. These are the numeric types, sequence types, and
-several others, including types themselves. There is no explicit
-Boolean type; use integers instead.
+several others, including types themselves.
\indexii{built-in}{types}
-\indexii{Boolean}{type}
Some operations are supported by several object types; in particular,
all objects can be compared, tested for truth value, and converted to
@@ -30,6 +28,9 @@
\item \code{None}
\withsubitem{(Built-in object)}{\ttindex{None}}
+\item \code{False}
+ \withsubitem{(Built-in object)}{\ttindex{False}}
+
\item zero of any numeric type, for example, \code{0}, \code{0L},
\code{0.0}, \code{0j}.
@@ -50,11 +51,12 @@
\index{true}
Operations and built-in functions that have a Boolean result always
-return \code{0} for false and \code{1} for true, unless otherwise
-stated. (Important exception: the Boolean operations
-\samp{or}\opindex{or} and \samp{and}\opindex{and} always return one of
-their operands.)
-
+return \code{0} or \code{False} for false and \code{1} or \code{True}
+for true, unless otherwise stated. (Important exception: the Boolean
+operations \samp{or}\opindex{or} and \samp{and}\opindex{and} always
+return one of their operands.)
+\index{False}
+\index{True}
\subsection{Boolean Operations \label{boolean}}
@@ -68,7 +70,7 @@
{if \var{x} is false, then \var{x}, else \var{y}}{(1)}
\hline
\lineiii{not \var{x}}
- {if \var{x} is false, then \code{1}, else \code{0}}{(2)}
+ {if \var{x} is false, then \code{True}, else \code{False}}{(2)}
\end{tableiii}
\opindex{and}
\opindex{or}
@@ -161,8 +163,10 @@
\subsection{Numeric Types \label{typesnumeric}}
-There are four numeric types: \dfn{plain integers}, \dfn{long integers},
+There are four distinct numeric types: \dfn{plain integers},
+\dfn{long integers},
\dfn{floating point numbers}, and \dfn{complex numbers}.
+In addition, Booleans are a subtype of plain integers.
Plain integers (also just called \dfn{integers})
are implemented using \ctype{long} in C, which gives them at least 32
bits of precision. Long integers have unlimited precision. Floating
@@ -170,6 +174,7 @@
their precision are off unless you happen to know the machine you are
working with.
\obindex{numeric}
+\obindex{Boolean}
\obindex{integer}
\obindex{long integer}
\obindex{floating point}
@@ -1389,6 +1394,22 @@
It is written as \code{Ellipsis}.
+\subsubsection{Boolean Values}
+
+Boolean values are the two constant objects \code{False} and
+\code{True}. They are used to represent truth values (although other
+values can also be considered false or true). In numeric contexts
+(for example when used as the argument to an arithmetic operator),
+they behave like the integers 0 and 1, respectively. The built-in
+function \function{bool()} can be used to cast any value to a Boolean,
+if the value can be interpreted as a truth value (see section Truth
+Value Testing above).
+
+They are written as \code{False} and \code{True}, respectively.
+\index{False}
+\index{True}
+\indexii{Boolean}{values}
+
\subsubsection{Internal Objects \label{typesinternal}}