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.
This commit is contained in:
Guido van Rossum 2002-04-03 22:41:51 +00:00
parent e9c0358bf4
commit 77f6a65eb0
29 changed files with 489 additions and 378 deletions

View file

@ -77,6 +77,16 @@ def my_import(name):
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

View file

@ -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 @@ The following values are considered false:
\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 @@ always true.
\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 @@ These are the Boolean operations, ordered by ascending priority:
{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 @@ only by sequence types (below).
\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 @@ point numbers are implemented using \ctype{double} in C. All bets on
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 @@ special operations. There is exactly one ellipsis object, named
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}}

View file

@ -162,7 +162,7 @@ complex numbers:
These represent elements from the mathematical set of whole numbers.
\obindex{integer}
There are two types of integers:
There are three types of integers:
\begin{description}
@ -187,6 +187,17 @@ represented in a variant of 2's complement which gives the illusion of
an infinite string of sign bits extending to the left.
\obindex{long integer}
\item[Booleans]
These represent the truth values False and True. The two objects
representing the values False and True are the only Boolean objects.
The Boolean type is a subtype of plain integers, and Boolean values
behave like the values 0 and 1, respectively, in almost all contexts,
the exception being that when converted to a string, the strings
\code{"False"} or \code{"True"} are returned, respectively.
\obindex{Boolean}
\ttindex{False}
\ttindex{True}
\end{description} % Integers
The rules for integer representation are intended to give the most
@ -222,6 +233,7 @@ and \code{z.imag}.
\end{description} % Numbers
\item[Sequences]
These represent finite ordered sets indexed by non-negative numbers.
The built-in function \function{len()}\bifuncindex{len} returns the
@ -1074,8 +1086,10 @@ wrong hash bucket).
\end{methoddesc}
\begin{methoddesc}[object]{__nonzero__}{self}
Called to implement truth value testing; should return \code{0} or
\code{1}. When this method is not defined, \method{__len__()} is
Called to implement truth value testing, and the built-in operation
\code{bool()}; should return \code{False} or \code{True}, or their
integer equivalents \code{0} or \code{1}.
When this method is not defined, \method{__len__()} is
called, if it is defined (see below). If a class defines neither
\method{__len__()} nor \method{__nonzero__()}, all its instances are
considered true.