字符串方法——原来的字符串模块的函数(在3.X中消失了)

未匹配的标注

2.X的内容暂不翻译

The history of Python’s string methods is somewhat
convoluted. For roughly the first decade of its existence,
Python provided a standard library module called string that
contained functions that largely mirrored the current set of
string object methods. By popular demand, in Python 2.0
these functions were made available as methods of string
objects. Because so many people had written so much code

that relied on the original string module, however, it was
retained for backward compatibility.
Today, you should use only string methods, not the original
string module. In fact, the original module call forms of
today’s string methods have been removed completely from
Python 3.X, and you should not use them in new code in
either 2.X or 3.X. However, because you may still see the
module in use in older Python 2.X code, and this text covers
both Pythons 2.X and 3.X, a brief look is in order here.
The upshot of this legacy is that in Python 2.X, there
technically are still two ways to invoke advanced string
operations: by calling object methods, or by calling string
module functions and passing in the objects as arguments.
For instance, given a variable X assigned to a string object,
calling an object method:

X.method(arguments)

is usually equivalent to calling the same operation through
the string module (provided that you have already imported
the module):

string.method(X, arguments)

Here’s an example of the method scheme in action:

>>> S = 'a+b+c+'
>>> x = S.replace('+', 'spam')
>>> x
'aspambspamcspam'

To access the same operation through the string module in
Python 2.X, you need to import the module (at least once in
your process) and pass in the object:

>>> import string
>>> y = string.replace(S, '+', 'spam')
>>> y
'aspambspamcspam'

Because the module approach was the standard for so long,
and because strings are such a central component of most
programs, you might see both call patterns in Python 2.X
code you come across.

Again, though, today you should always use method calls
instead of the older module calls. There are good reasons for
this, besides the fact that the module calls have gone away in
3.X. For one thing, the module call scheme requires you to
import the string module (methods do not require imports).
For another, the module makes calls a few characters longer
to type (when you load the module with import, that is, not
using from). And, finally, the module runs more slowly than
methods (the module maps most calls back to the methods
and so incurs an extra call along the way).

The original string module itself, without its string method
equivalents, is retained in Python 3.X because it contains
additional tools, including predefined string constants (e.g.,
string.digits) and a Template object system—a relatively
obscure formatting tool that predates the string format
method and is largely omitted here (for details, see the brief
note comparing it to other formatting tools ahead, as well as
Python’s library manual). Unless you really want to have to
change your 2.X code to use 3.X, though, you should consider
any basic string operation calls in it to be just ghosts of
Python past.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 查看所有版本


暂无话题~