mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
validation typos
This commit is contained in:
@@ -1114,7 +1114,7 @@ tests\Big5\0804.blogspot.com.xml</samp>
|
||||
File "C:\home\chardet\chardet\latin1prober.py", line 126, in get_confidence
|
||||
total = reduce(operator.add, self._mFreqCounter)
|
||||
NameError: global name 'reduce' is not defined</samp></pre>
|
||||
<p>According to the official <a href=http://docs.python.org/dev/3.0/whatsnew/3.0.html#builtins>What's New In Python 3.0</a> guide, the <code>reduce()</code> function has been moved out of the global namespace and into the <code>functools</code> module. Quoting the guide: "Use <code>functools.reduce()</code> if you really need it; however, 99 percent of the time an explicit <code>for</code> loop is more readable." You can read more about the decision from Guido van Rossum's weblog: <a href=http://www.artima.com/weblogs/viewpost.jsp?thread=98196>The fate of reduce() in Python 3000</a>.
|
||||
<p>According to the official <a href=http://docs.python.org/dev/3.0/whatsnew/3.0.html#builtins>What's New In Python 3.0</a> guide, the <code>reduce()</code> function has been moved out of the global namespace and into the <code>functools</code> module. Quoting the guide: "Use <code>functools.reduce()</code> if you really need it; however, 99 percent of the time an explicit <code>for</code> loop is more readable." You can read more about the decision from Guido van Rossum's weblog: <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=98196">The fate of reduce() in Python 3000</a>.
|
||||
<pre><code>def get_confidence(self):
|
||||
if self.get_state() == constants.eNotMe:
|
||||
return 0.01
|
||||
|
||||
@@ -416,7 +416,7 @@ def plural(noun):
|
||||
<p>Thoroughly confused yet? Excellent. Let’s see how to call this iterator:</p>
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd>from fibonacci2 import fib</kbd></a>
|
||||
<samp class=p>>>> </samp><kbd>from fibonacci2 import fib</kbd>
|
||||
<samp class=p>>>> </samp><kbd>for n in fib(1000):</kbd>
|
||||
<samp class=p>... </samp><kbd> print(n, end=' ')</kbd>
|
||||
<samp>0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</samp></pre>
|
||||
|
||||
+3
-3
@@ -120,13 +120,13 @@ def approximate_size(size, a_kilobyte_is_1024_bytes=True):
|
||||
Returns: string
|
||||
|
||||
<a> """ <span>③</span></a>
|
||||
if size < 0:
|
||||
if size < 0:
|
||||
<a> raise ValueError('number must be non-negative') <span>④</span></a>
|
||||
|
||||
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
|
||||
for suffix in SUFFIXES[multiple]:
|
||||
size /= multiple
|
||||
if size < multiple:
|
||||
if size < multiple:
|
||||
<a> return "{0:.1f} {1}".format(size, suffix) <span>⑤</span></a>
|
||||
|
||||
raise ValueError('number too large')</code></pre>
|
||||
@@ -203,7 +203,7 @@ def approximate_size(size, a_kilobyte_is_1024_bytes=True):
|
||||
|
||||
<p>But wait! There's more! Let's take another look at that strange line of code from <code>humansize.py</code>:
|
||||
|
||||
<pre><code>if size < multiple:
|
||||
<pre><code>if size < multiple:
|
||||
return "{0:.1f} {1}".format(size, suffix)</code></pre>
|
||||
|
||||
<p><code>{1}</code> is replaced with the second argument passed to the <code>format()</code> method, which is <var>suffix</var>. But what is <code>{0:.1f}</code>? It's two things: <code>{0}</code>, which you recognize, and <code>:.1f</code>, which you don't. The second half (including and after the colon) defines the <i>format specifier</i>, which further refines how the replaced variable should be formatted.
|
||||
|
||||
Reference in New Issue
Block a user