mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
fixed inconsistent text references to functions, and a few other typos [h/t G.P.]
This commit is contained in:
@@ -44,8 +44,8 @@ def plural(noun):
|
||||
else:
|
||||
return noun + 's'</code></pre>
|
||||
<ol>
|
||||
<li>This is a regular expression, but it uses a syntax you didn’t see in <a href=regular-expressions.html><i>Regular Expressions</i></a>. The square brackets mean “match exactly one of these characters.” So <code>[sxz]</code> means “<code>s</code>, or <code>x</code>, or <code>z</code>”, but only one of them. The <code>$</code> should be familiar; it matches the end of string. Combined, this regular expression is tests whether <var>noun</var> ends with <code>s</code>, <code>x</code>, or <code>z</code>.
|
||||
<li>This <code>re.sub</code> function performs regular expression-based string substitutions.
|
||||
<li>This is a regular expression, but it uses a syntax you didn’t see in <a href=regular-expressions.html><i>Regular Expressions</i></a>. The square brackets mean “match exactly one of these characters.” So <code>[sxz]</code> means “<code>s</code>, or <code>x</code>, or <code>z</code>”, but only one of them. The <code>$</code> should be familiar; it matches the end of string. Combined, this regular expression tests whether <var>noun</var> ends with <code>s</code>, <code>x</code>, or <code>z</code>.
|
||||
<li>This <code>re.sub()</code> function performs regular expression-based string substitutions.
|
||||
</ol>
|
||||
|
||||
<p>Let’s look at regular expression substitutions in more detail.
|
||||
@@ -177,7 +177,7 @@ def plural(noun):
|
||||
if match_default(noun):
|
||||
return apply_default(noun)</code></pre>
|
||||
|
||||
<p>The benefit here is that that <code>plural</code> function is now simplified. It takes a list of rules, defined elsewhere, and iterates through them in a generic fashion.
|
||||
<p>The benefit here is that the <code>plural()</code> function is now simplified. It takes a list of rules, defined elsewhere, and iterates through them in a generic fashion.
|
||||
|
||||
<ol>
|
||||
<li>Get a match rule
|
||||
@@ -187,7 +187,7 @@ def plural(noun):
|
||||
|
||||
<p>The rules could be defined anywhere, in any way. The <code>plural()</code> function doesn’t care.
|
||||
|
||||
<p>Now, was adding this level of abstraction worth it? Well, not yet. Let’s consider what it would take to add a new rule to the function. In the first example, it would require adding an <code>if</code> statement to the <code>plural</code> function. In this second example, it would require adding two functions, <code>match_foo()</code> and <code>apply_foo()</code>, and then updating the <var>rules</var> list to specify where in the order the new match and apply functions should be called relative to the other rules.
|
||||
<p>Now, was adding this level of abstraction worth it? Well, not yet. Let’s consider what it would take to add a new rule to the function. In the first example, it would require adding an <code>if</code> statement to the <code>plural()</code> function. In this second example, it would require adding two functions, <code>match_foo()</code> and <code>apply_foo()</code>, and then updating the <var>rules</var> list to specify where in the order the new match and apply functions should be called relative to the other rules.
|
||||
|
||||
<p>But this is really just a stepping stone to the next section. Let’s move on…
|
||||
|
||||
@@ -205,9 +205,9 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
return re.sub(search, replace, word)
|
||||
<a> return (matches_rule, apply_rule) <span>③</span></a></code></pre>
|
||||
<ol>
|
||||
<li><code>build_match_and_apply_functions</code> is a function that builds other functions dynamically. It takes <var>pattern</var>, <var>search</var> and <var>replace</var>, then defines a <code>matches_rule()</code> function which calls <code>re.search()</code> with the <var>pattern</var> that was passed to the <code>build_match_and_apply_functions()</code> function, and the <var>word</var> that was passed to the <code>matches_rule()</code> function you’re building. Whoa.
|
||||
<li>Building the apply function works the same way. The apply function is a function that takes one parameter, and calls <code>re.sub()</code> with the <var>search</var> and <var>replace</var> parameters that were passed to the <code>build_match_and_apply_functions</code> function, and the <var>word</var> that was passed to the <code>apply_rule()</code> function you’re building. This technique of using the values of outside parameters within a dynamic function is called <em>closures</em>. You’re essentially defining constants within the apply function you’re building: it takes one parameter (<var>word</var>), but it then acts on that plus two other values (<var>search</var> and <var>replace</var>) which were set when you defined the apply function.
|
||||
<li>Finally, the <code>build_match_and_apply_functions</code> function returns a tuple of two values: the two functions you just created. The constants you defined within those functions (<var>pattern</var> within <var>matchFunction</var>, and <var>search</var> and <var>replace</var> within <var>applyFunction</var>) stay with those functions, even after you return from <code>build_match_and_apply_functions</code>. That’s insanely cool.
|
||||
<li><code>build_match_and_apply_functions()</code> is a function that builds other functions dynamically. It takes <var>pattern</var>, <var>search</var> and <var>replace</var>, then defines a <code>matches_rule()</code> function which calls <code>re.search()</code> with the <var>pattern</var> that was passed to the <code>build_match_and_apply_functions()</code> function, and the <var>word</var> that was passed to the <code>matches_rule()</code> function you’re building. Whoa.
|
||||
<li>Building the apply function works the same way. The apply function is a function that takes one parameter, and calls <code>re.sub()</code> with the <var>search</var> and <var>replace</var> parameters that were passed to the <code>build_match_and_apply_functions()</code> function, and the <var>word</var> that was passed to the <code>apply_rule()</code> function you’re building. This technique of using the values of outside parameters within a dynamic function is called <em>closures</em>. You’re essentially defining constants within the apply function you’re building: it takes one parameter (<var>word</var>), but it then acts on that plus two other values (<var>search</var> and <var>replace</var>) which were set when you defined the apply function.
|
||||
<li>Finally, the <code>build_match_and_apply_functions()</code> function returns a tuple of two values: the two functions you just created. The constants you defined within those functions (<var>pattern</var> within <var>matchFunction</var>, and <var>search</var> and <var>replace</var> within <var>applyFunction</var>) stay with those functions, even after you return from <code>build_match_and_apply_functions()</code>. That’s insanely cool.
|
||||
</ol>
|
||||
|
||||
<p>If this is incredibly confusing (and it should be, this is weird stuff), it may become clearer when you see how to use it.
|
||||
@@ -224,7 +224,7 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
for (pattern, search, replace) in patterns]</code></pre>
|
||||
<ol>
|
||||
<li>Our pluralization rules are now defined as a list of lists of strings (not functions). The first string in each group is the regular expression pattern that you would use in <code>re.search()</code> to see if this rule matches. The second and third strings in each group are the search and replace expressions you would use in <code>re.sub()</code> to actually apply the rule to turn a noun into its plural.
|
||||
<li>This line is magic. It takes the list of strings in <var>patterns</var> and turns them into a list of functions. How? By mapping the strings to the <code>build_match_and_apply_functions</code> function, which just happens to take three strings as parameters and return a tuple of two functions. This means that <var>rules</var> ends up being exactly the same as the previous example: a list of tuples, where each tuple is a pair of functions, where the first function is the match function that calls <code>re.search()</code>, and the second function is the apply function that calls <code>re.sub()</code>.
|
||||
<li>This line is magic. It takes the list of strings in <var>patterns</var> and turns them into a list of functions. How? By mapping the strings to the <code>build_match_and_apply_functions()</code> function, which just happens to take three strings as parameters and return a tuple of two functions. This means that <var>rules</var> ends up being exactly the same as the previous example: a list of tuples, where each tuple is a pair of functions, where the first function is the match function that calls <code>re.search()</code>, and the second function is the apply function that calls <code>re.sub()</code>.
|
||||
</ol>
|
||||
|
||||
<p>Rounding out this version of the script is the main entry point, the <code>plural()</code> function.
|
||||
@@ -234,7 +234,7 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
if matches_rule(noun):
|
||||
return apply_rule(noun)</code></pre>
|
||||
<ol>
|
||||
<li>Since the <var>rules</var> list is the same as the previous example (really, it is), it should come as no surprise that the <code>plural()</code> function hasn’t changed at all. It’s completely generic; it takes a list of rule functions and calls them in order. It doesn’t care how the rules are defined. In the previous example, they were defined as seperate named functions. Now they are built dynamically by mapping the output of the <code>build_match_and_apply_functions()</code> function onto a list of raw strings. It doesn’t matter; the <code>plural</code> function still works the same way.
|
||||
<li>Since the <var>rules</var> list is the same as the previous example (really, it is), it should come as no surprise that the <code>plural()</code> function hasn’t changed at all. It’s completely generic; it takes a list of rule functions and calls them in order. It doesn’t care how the rules are defined. In the previous example, they were defined as seperate named functions. Now they are built dynamically by mapping the output of the <code>build_match_and_apply_functions()</code> function onto a list of raw strings. It doesn’t matter; the <code>plural()</code> function still works the same way.
|
||||
</ol>
|
||||
|
||||
<h2 id=a-file-of-patterns>A File Of Patterns</h2>
|
||||
|
||||
Reference in New Issue
Block a user