| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -165,7 +165,7 @@ def __rcmp__(self, other): | |||
| 165 | 165 | other = ToComplex(other) | |
| 166 | 166 | return cmp(other, self) | |
| 167 | 167 | ||
| 168 | - def __nonzero__(self): | ||
| 168 | + def __bool__(self): | ||
| 169 | 169 | return not (self.re == self.im == 0) | |
| 170 | 170 | ||
| 171 | 171 | abs = radius = __abs__ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -223,7 +223,7 @@ def __rcmp__(b, a): | |||
| 223 | 223 | return cmp(Rat(a), b) | |
| 224 | 224 | ||
| 225 | 225 | # a != 0 | |
| 226 | - def __nonzero__(a): | ||
| 226 | + def __bool__(a): | ||
| 227 | 227 | return a.__num != 0 | |
| 228 | 228 | ||
| 229 | 229 | # coercion | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,7 +55,7 @@ \section{\module{operator} --- | |||
| 55 | 55 | Return the outcome of \keyword{not} \var{o}. (Note that there is no | |
| 56 | 56 | \method{__not__()} method for object instances; only the interpreter | |
| 57 | 57 | core defines this operation. The result is affected by the | |
| 58 | - \method{__nonzero__()} and \method{__len__()} methods.) | ||
| 58 | + \method{__bool__()} and \method{__len__()} methods.) | ||
| 59 | 59 | \end{funcdesc} | |
| 60 | 60 | ||
| 61 | 61 | \begin{funcdesc}{truth}{o} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,7 +51,7 @@ \section{Truth Value Testing\label{truth}} | |||
| 51 | 51 | \item any empty mapping, for example, \code{\{\}}. | |
| 52 | 52 | ||
| 53 | 53 | \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 | ||
| 55 | 55 | method returns the integer zero or \class{bool} value | |
| 56 | 56 | \code{False}.\footnote{Additional | |
| 57 | 57 | information on these special methods may be found in the | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -162,44 +162,44 @@ \subsection{Examples} | |||
| 162 | 162 | missing and present object attributes. | |
| 163 | 163 | ||
| 164 | 164 | \begin{verbatim} | |
| 165 | - % timeit.py 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass' | ||
| 165 | + % timeit.py 'try:' ' str.__bool__' 'except AttributeError:' ' pass' | ||
| 166 | 166 | 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' | ||
| 168 | 168 | 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' | ||
| 170 | 170 | 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' | ||
| 172 | 172 | 100000 loops, best of 3: 2.23 usec per loop | |
| 173 | 173 | \end{verbatim} | |
| 174 | 174 | ||
| 175 | 175 | \begin{verbatim} | |
| 176 | 176 | >>> import timeit | |
| 177 | 177 | >>> s = """\ | |
| 178 | 178 | ... try: | |
| 179 | - ... str.__nonzero__ | ||
| 179 | + ... str.__bool__ | ||
| 180 | 180 | ... except AttributeError: | |
| 181 | 181 | ... pass | |
| 182 | 182 | ... """ | |
| 183 | 183 | >>> t = timeit.Timer(stmt=s) | |
| 184 | 184 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) | |
| 185 | 185 | 17.09 usec/pass | |
| 186 | 186 | >>> s = """\ | |
| 187 | - ... if hasattr(str, '__nonzero__'): pass | ||
| 187 | + ... if hasattr(str, '__bool__'): pass | ||
| 188 | 188 | ... """ | |
| 189 | 189 | >>> t = timeit.Timer(stmt=s) | |
| 190 | 190 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) | |
| 191 | 191 | 4.85 usec/pass | |
| 192 | 192 | >>> s = """\ | |
| 193 | 193 | ... try: | |
| 194 | - ... int.__nonzero__ | ||
| 194 | + ... int.__bool__ | ||
| 195 | 195 | ... except AttributeError: | |
| 196 | 196 | ... pass | |
| 197 | 197 | ... """ | |
| 198 | 198 | >>> t = timeit.Timer(stmt=s) | |
| 199 | 199 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) | |
| 200 | 200 | 1.97 usec/pass | |
| 201 | 201 | >>> s = """\ | |
| 202 | - ... if hasattr(int, '__nonzero__'): pass | ||
| 202 | + ... if hasattr(int, '__bool__'): pass | ||
| 203 | 203 | ... """ | |
| 204 | 204 | >>> t = timeit.Timer(stmt=s) | |
| 205 | 205 | >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -375,7 +375,7 @@ \subsection{Registry Handle Objects \label{handle-object}} | |||
| 375 | 375 | also accept an integer, however, use of the handle object is | |
| 376 | 376 | encouraged. | |
| 377 | 377 | ||
| 378 | - Handle objects provide semantics for \method{__nonzero__()} - thus | ||
| 378 | + Handle objects provide semantics for \method{__bool__()} - thus | ||
| 379 | 379 | \begin{verbatim} | |
| 380 | 380 | if handle: | |
| 381 | 381 | print "Yes" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,7 +171,7 @@ \subsection{Boolean Objects \label{boolean-objects}} | |||
| 171 | 171 | This class may be initialized from any Python value; the instance | |
| 172 | 172 | returned depends only on its truth value. It supports various Python | |
| 173 | 173 | operators through \method{__cmp__()}, \method{__repr__()}, | |
| 174 | - \method{__int__()}, and \method{__nonzero__()} methods, all | ||
| 174 | + \method{__int__()}, and \method{__bool__()} methods, all | ||
| 175 | 175 | implemented in the obvious ways. | |
| 176 | 176 | ||
| 177 | 177 | It also has the following method, supported mainly for internal use by | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1313,13 +1313,13 @@ \subsection{Basic customization\label{customization}} | |||
| 1313 | 1313 | \withsubitem{(object method)}{\ttindex{__cmp__()}} | |
| 1314 | 1314 | \end{methoddesc} | |
| 1315 | 1315 | ||
| 1316 | - \begin{methoddesc}[object]{__nonzero__}{self} | ||
| 1316 | + \begin{methoddesc}[object]{__bool__}{self} | ||
| 1317 | 1317 | 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}. | ||
| 1320 | 1319 | 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 | ||
| 1323 | 1323 | considered true. | |
| 1324 | 1324 | \withsubitem{(mapping object method)}{\ttindex{__len__()}} | |
| 1325 | 1325 | \end{methoddesc} | |
@@ -1693,9 +1693,9 @@ \subsection{Emulating container types\label{sequence-types}} | |||
| 1693 | 1693 | Called to implement the built-in function | |
| 1694 | 1694 | \function{len()}\bifuncindex{len}. Should return the length of the | |
| 1695 | 1695 | 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 | ||
| 1697 | 1697 | returns zero is considered to be false in a Boolean context. | |
| 1698 | - \withsubitem{(object method)}{\ttindex{__nonzero__()}} | ||
| 1698 | + \withsubitem{(object method)}{\ttindex{__bool__()}} | ||
| 1699 | 1699 | \end{methoddesc} | |
| 1700 | 1700 | ||
| 1701 | 1701 | \begin{methoddesc}[container object]{__getitem__}{self, key} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -160,7 +160,7 @@ typedef struct { | |||
| 160 | 160 | unaryfunc nb_negative; | |
| 161 | 161 | unaryfunc nb_positive; | |
| 162 | 162 | unaryfunc nb_absolute; | |
| 163 | - inquiry nb_nonzero; | ||
| 163 | + inquiry nb_bool; | ||
| 164 | 164 | unaryfunc nb_invert; | |
| 165 | 165 | binaryfunc nb_lshift; | |
| 166 | 166 | binaryfunc nb_rshift; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -633,14 +633,14 @@ def _check_nans(self, other = None, context=None): | |||
| 633 | 633 | return other | |
| 634 | 634 | return 0 | |
| 635 | 635 | ||
| 636 | - def __nonzero__(self): | ||
| 636 | + def __bool__(self): | ||
| 637 | 637 | """Is the number non-zero? | |
| 638 | 638 | ||
| 639 | 639 | 0 if self == 0 | |
| 640 | 640 | 1 if self != 0 | |
| 641 | 641 | """ | |
| 642 | 642 | if self._is_special: | |
| 643 | - return 1 | ||
| 643 | + return True | ||
| 644 | 644 | return sum(self._int) != 0 | |
| 645 | 645 | ||
| 646 | 646 | def __cmp__(self, other, context=None): | |
@@ -759,7 +759,7 @@ def __hash__(self): | |||
| 759 | 759 | i = int(self) | |
| 760 | 760 | if self == Decimal(i): | |
| 761 | 761 | return hash(i) | |
| 762 | - assert self.__nonzero__() # '-0' handled by integer case | ||
| 762 | + assert self.__bool__() # '-0' handled by integer case | ||
| 763 | 763 | return hash(str(self.normalize())) | |
| 764 | 764 | ||
| 765 | 765 | def as_tuple(self): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments