Skip to content

Commit 4dafcc4

Browse files
committed
- patch #1600346 submitted by Tomer Filiba
- Renamed nb_nonzero slots to nb_bool - Renamed __nonzero__ methods to __bool__ - update core, lib, docs, and tests to match
1 parent dfc9d4f commit 4dafcc4

31 files changed

+118
-82
lines changed

Demo/classes/Complex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def __rcmp__(self, other):
165165
other = ToComplex(other)
166166
return cmp(other, self)
167167

168-
def __nonzero__(self):
168+
def __bool__(self):
169169
return not (self.re == self.im == 0)
170170

171171
abs = radius = __abs__

Demo/classes/Rat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def __rcmp__(b, a):
223223
return cmp(Rat(a), b)
224224

225225
# a != 0
226-
def __nonzero__(a):
226+
def __bool__(a):
227227
return a.__num != 0
228228

229229
# coercion

Doc/lib/liboperator.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ \section{\module{operator} ---
5555
Return the outcome of \keyword{not} \var{o}. (Note that there is no
5656
\method{__not__()} method for object instances; only the interpreter
5757
core defines this operation. The result is affected by the
58-
\method{__nonzero__()} and \method{__len__()} methods.)
58+
\method{__bool__()} and \method{__len__()} methods.)
5959
\end{funcdesc}
6060

6161
\begin{funcdesc}{truth}{o}

Doc/lib/libstdtypes.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ \section{Truth Value Testing\label{truth}}
5151
\item any empty mapping, for example, \code{\{\}}.
5252

5353
\item instances of user-defined classes, if the class defines a
54-
\method{__nonzero__()} or \method{__len__()} method, when that
54+
\method{__bool__()} or \method{__len__()} method, when that
5555
method returns the integer zero or \class{bool} value
5656
\code{False}.\footnote{Additional
5757
information on these special methods may be found in the

Doc/lib/libtimeit.tex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,44 +162,44 @@ \subsection{Examples}
162162
missing and present object attributes.
163163

164164
\begin{verbatim}
165-
% timeit.py 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass'
165+
% timeit.py 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
166166
100000 loops, best of 3: 15.7 usec per loop
167-
% timeit.py 'if hasattr(str, "__nonzero__"): pass'
167+
% timeit.py 'if hasattr(str, "__bool__"): pass'
168168
100000 loops, best of 3: 4.26 usec per loop
169-
% timeit.py 'try:' ' int.__nonzero__' 'except AttributeError:' ' pass'
169+
% timeit.py 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
170170
1000000 loops, best of 3: 1.43 usec per loop
171-
% timeit.py 'if hasattr(int, "__nonzero__"): pass'
171+
% timeit.py 'if hasattr(int, "__bool__"): pass'
172172
100000 loops, best of 3: 2.23 usec per loop
173173
\end{verbatim}
174174

175175
\begin{verbatim}
176176
>>> import timeit
177177
>>> s = """\
178178
... try:
179-
... str.__nonzero__
179+
... str.__bool__
180180
... except AttributeError:
181181
... pass
182182
... """
183183
>>> t = timeit.Timer(stmt=s)
184184
>>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
185185
17.09 usec/pass
186186
>>> s = """\
187-
... if hasattr(str, '__nonzero__'): pass
187+
... if hasattr(str, '__bool__'): pass
188188
... """
189189
>>> t = timeit.Timer(stmt=s)
190190
>>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
191191
4.85 usec/pass
192192
>>> s = """\
193193
... try:
194-
... int.__nonzero__
194+
... int.__bool__
195195
... except AttributeError:
196196
... pass
197197
... """
198198
>>> t = timeit.Timer(stmt=s)
199199
>>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
200200
1.97 usec/pass
201201
>>> s = """\
202-
... if hasattr(int, '__nonzero__'): pass
202+
... if hasattr(int, '__bool__'): pass
203203
... """
204204
>>> t = timeit.Timer(stmt=s)
205205
>>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)

Doc/lib/libwinreg.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ \subsection{Registry Handle Objects \label{handle-object}}
375375
also accept an integer, however, use of the handle object is
376376
encouraged.
377377

378-
Handle objects provide semantics for \method{__nonzero__()} - thus
378+
Handle objects provide semantics for \method{__bool__()} - thus
379379
\begin{verbatim}
380380
if handle:
381381
print "Yes"

Doc/lib/libxmlrpclib.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ \subsection{Boolean Objects \label{boolean-objects}}
171171
This class may be initialized from any Python value; the instance
172172
returned depends only on its truth value. It supports various Python
173173
operators through \method{__cmp__()}, \method{__repr__()},
174-
\method{__int__()}, and \method{__nonzero__()} methods, all
174+
\method{__int__()}, and \method{__bool__()} methods, all
175175
implemented in the obvious ways.
176176

177177
It also has the following method, supported mainly for internal use by

Doc/ref/ref3.tex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,13 +1313,13 @@ \subsection{Basic customization\label{customization}}
13131313
\withsubitem{(object method)}{\ttindex{__cmp__()}}
13141314
\end{methoddesc}
13151315

1316-
\begin{methoddesc}[object]{__nonzero__}{self}
1316+
\begin{methoddesc}[object]{__bool__}{self}
13171317
Called to implement truth value testing, and the built-in operation
1318-
\code{bool()}; should return \code{False} or \code{True}, or their
1319-
integer equivalents \code{0} or \code{1}.
1318+
\code{bool()}; should return \code{False} or \code{True}.
13201319
When this method is not defined, \method{__len__()} is
1321-
called, if it is defined (see below). If a class defines neither
1322-
\method{__len__()} nor \method{__nonzero__()}, all its instances are
1320+
called, if it is defined (see below) and \code{True} is returned when
1321+
the length is not zero. If a class defines neither
1322+
\method{__len__()} nor \method{__bool__()}, all its instances are
13231323
considered true.
13241324
\withsubitem{(mapping object method)}{\ttindex{__len__()}}
13251325
\end{methoddesc}
@@ -1693,9 +1693,9 @@ \subsection{Emulating container types\label{sequence-types}}
16931693
Called to implement the built-in function
16941694
\function{len()}\bifuncindex{len}. Should return the length of the
16951695
object, an integer \code{>=} 0. Also, an object that doesn't define a
1696-
\method{__nonzero__()} method and whose \method{__len__()} method
1696+
\method{__bool__()} method and whose \method{__len__()} method
16971697
returns zero is considered to be false in a Boolean context.
1698-
\withsubitem{(object method)}{\ttindex{__nonzero__()}}
1698+
\withsubitem{(object method)}{\ttindex{__bool__()}}
16991699
\end{methoddesc}
17001700

17011701
\begin{methoddesc}[container object]{__getitem__}{self, key}

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ typedef struct {
160160
unaryfunc nb_negative;
161161
unaryfunc nb_positive;
162162
unaryfunc nb_absolute;
163-
inquiry nb_nonzero;
163+
inquiry nb_bool;
164164
unaryfunc nb_invert;
165165
binaryfunc nb_lshift;
166166
binaryfunc nb_rshift;

Lib/decimal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,14 @@ def _check_nans(self, other = None, context=None):
633633
return other
634634
return 0
635635

636-
def __nonzero__(self):
636+
def __bool__(self):
637637
"""Is the number non-zero?
638638
639639
0 if self == 0
640640
1 if self != 0
641641
"""
642642
if self._is_special:
643-
return 1
643+
return True
644644
return sum(self._int) != 0
645645

646646
def __cmp__(self, other, context=None):
@@ -759,7 +759,7 @@ def __hash__(self):
759759
i = int(self)
760760
if self == Decimal(i):
761761
return hash(i)
762-
assert self.__nonzero__() # '-0' handled by integer case
762+
assert self.__bool__() # '-0' handled by integer case
763763
return hash(str(self.normalize()))
764764

765765
def as_tuple(self):

0 commit comments

Comments
 (0)