|You are here: Home > Dive Into Python > Dynamic functions > plural.py, stage 4||<< >>|
Python from novice to pro
Let's factor out the duplication in the code so that defining new rules can be easier.
Example 17.9. plural4.py
import re def buildMatchAndApplyFunctions((pattern, search, replace)): matchFunction = lambda word: re.search(pattern, word) applyFunction = lambda word: re.sub(search, replace, word) return (matchFunction, applyFunction)
If this is incredibly confusing (and it should be, this is weird stuff), it may become clearer when you see how to use it.
Example 17.10. plural4.py continued
patterns = \ ( ('[sxz]$', '$', 'es'), ('[^aeioudgkprt]h$', '$', 'es'), ('(qu|[^aeiou])y$', 'y$', 'ies'), ('$', '$', 's') ) rules = map(buildMatchAndApplyFunctions, patterns)
I swear I am not making this up: rules ends up with exactly the same list of functions as the previous example. Unroll the rules definition, and you'll get this:
Example 17.11. Unrolling the rules definition
rules = \ ( ( lambda word: re.search('[sxz]$', word), lambda word: re.sub('$', 'es', word) ), ( lambda word: re.search('[^aeioudgkprt]h$', word), lambda word: re.sub('$', 'es', word) ), ( lambda word: re.search('[^aeiou]y$', word), lambda word: re.sub('y$', 'ies', word) ), ( lambda word: re.search('$', word), lambda word: re.sub('$', 's', word) ) )
Example 17.12. plural4.py, finishing up
|Since the rules list is the same as the previous example, it should come as no surprise that the plural function hasn't changed. Remember, 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 stage 2, they were defined as seperate named functions. In stage 3, they were defined as anonymous lambda functions. Now in stage 4, they are built dynamically by mapping the buildMatchAndApplyFunctions function onto a list of raw strings. Doesn't matter; the plural function still works the same way.|
Just in case that wasn't mind-blowing enough, I must confess that there was a subtlety in the definition of buildMatchAndApplyFunctions that I skipped over. Let's go back and take another look.
Example 17.13. Another look at buildMatchAndApplyFunctions
Example 17.14. Expanding tuples when calling functions
>>> def foo((a, b, c)): ... print c ... print b ... print a >>> parameters = ('apple', 'bear', 'catnap') >>> foo(parameters) catnap bear apple
|The proper way to call the function foo is with a tuple of three elements. When the function is called, the elements are assigned to different local variables within foo.|
Now let's go back and see why this auto-tuple-expansion trick was necessary. patterns was a list of tuples, and each tuple had three elements. When you called map(buildMatchAndApplyFunctions, patterns), that means that buildMatchAndApplyFunctions is not getting called with three parameters. Using map to map a single list onto a function always calls the function with a single parameter: each element of the list. In the case of patterns, each element of the list is a tuple, so buildMatchAndApplyFunctions always gets called with the tuple, and you use the auto-tuple-expansion trick in the definition of buildMatchAndApplyFunctions to assign the elements of that tuple to named variables that you can work with.
<< plural.py, stage 3
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
plural.py, stage 5 >>