diff options
author | Guido van Rossum <guido@python.org> | 2002-04-03 22:41:51 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-04-03 22:41:51 (GMT) |
commit | 77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd (patch) | |
tree | e92163095e7ae548c36cea459dad87db74a413ef /Doc | |
parent | e9c0358bf45bd6e0fe0b17720b41d20d618e6d9d (diff) | |
download | cpython-77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd.zip cpython-77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd.tar.gz cpython-77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd.tar.bz2 |
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.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/lib/libfuncs.tex | 10 | ||||
-rw-r--r-- | Doc/lib/libstdtypes.tex | 41 | ||||
-rw-r--r-- | Doc/ref/ref3.tex | 20 |
3 files changed, 58 insertions, 13 deletions
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 @@ 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 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 @@ 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}} diff --git a/Doc/ref/ref3.tex b/Doc/ref/ref3.tex index d355b69..2056a6c 100644 --- a/Doc/ref/ref3.tex +++ b/Doc/ref/ref3.tex @@ -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. |