[Python-Dev] String module

Raymond Hettinger python@rcn.com
Wed, 29 May 2002 18:51:02 -0400


From: "Alex Martelli" <aleax@aleax.it>

> > strings.  That way, we can get O(1) behavior instead of O(n) behavior
for
> > code like:  if c in str.printable:  c='*'.   If someone needs to know
the
> > contents, they can run str.printable.keys().  Also, because the
dictionary
> > is mutable, someone can (at runtime) expand or contract the definitions:
> > str.whitespace.append('_').
>
> append would of course not work on a dictionary, but the prospect of
> allowing easy mutation of fundamental built-ins is quite negative in
Python --
> goes against the grain of the language.  A read-only dictionary might be
OK.

I like the read-only dictionary better than the boolean test methods.  It
minimizes the effort in upgrading existing code of the form:

for c in string.lowercase:
    do something
if c in string.lowercase:
    do something

I can global search/replace string.lowercase with str.lower in dictionary
form and everything will run fine (and faster too).

Also, I like the mapping because provides a way to see the membership.  With
the string form or the mapping form, it's easy to find-out exactly what is
defined as whitespace.  If there is a C coded boolean test, I have to filter
the whole alphabet or look it up in the docs.

If we can get some agreement that this is the way to go, I can work with the
OP on a revised patch so we get the string module silently deprecated.


Raymond Hettinger