[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

field to explore, but holmes is probably best left to the foggy mists of Python prehistory
(and souls brave enough to try the port).


Lesson 1: Prototype and Migrate
If you care about performance, try to use the string object’s methods rather than things
such as regular expressions whenever you can. Although this is a broad rule of thumb
and can vary from release to release, some string methods may be faster because they
have less work to do.
In fact, we can learn something from Python’s own history here. Today’s string object
methods began life as Python-coded functions in the original string module. Due to
their prevalence, they were later optimized by moving their implementation to the C
language. When you imported string, it internally replaced most of its content with
functions imported from the strop C extension module; strop methods were reportedly
100 to 1,000 times faster than their Python-coded equivalents at the time.
The result was dramatically faster performance for string client programs without im-
pacting the interface. That is, string module clients became instantly faster without
having to be modified for the new C-based module. Of course, these operations evolved
further and were finally moved to string object methods, their only form today. But this
reflects a common pattern in Python work. A similar migration path was applied to
the pickle module we met in Chapter 17—the later cPickle recoding in Python 2.X
and _pickle in 3.X are compatible but much faster.
This is a great lesson about Python development: modules can be coded quickly in
Python at first and translated to C later for efficiency if required. Because the interface
to Python and C extension modules is identical (both are imported modules with call-
able function attributes), C translations of modules are backward compatible with their
Python prototypes. The only impact of the translation of such modules on clients usu-
ally is an improvement in performance.
There is normally no need to move every module to C for delivery of an application:
you can pick and choose performance-critical modules (such as string and pickle) for
translation and leave others coded in Python. Use the timing and profiling techniques
discussed in Chapter 18 to isolate which modules will give the most improvement when
translated to C. Once you do, the next chapter shows how to go about writing C-based
extension modules.

Regular Expression Pattern Matching


Splitting and joining strings is a simple way to process text, as long as it follows the
format you expect. For more general text analysis tasks where the structure of your
data is not so rigidly defined, Python provides regular expression matching utilities.
Especially for the kinds of text associated with domains such as the Internet and data-
bases today, this flexibility can be a powerful ally.


Regular Expression Pattern Matching | 1415
Free download pdf