[Python-checkins] String methods... finally

Barry A. Warsaw bwarsaw@cnri.reston.va.us (Barry A. Warsaw)
Tue, 15 Jun 1999 17:53:13 -0400 (EDT)


I've finally checked my string methods changes into the Python source
tree, albeit on a CVS branch (see below).  These changes are
outgrowths of discussions we've had on the string-sig and other
forums, with Greg Stein giving lots of very useful early feedback.  I
call these changes controversial (hence the branch) because Guido
hasn't had much opportunity to play with them yet.  Now that he -- and
you -- can check them out, I'm sure I'll get lots more feedback!

First, to check them out you need to switch to the string_methods CVS
branch.  On Un*x:

    cvs update -r string_methods

You might want to do this in a separate tree because this will sticky
tag your tree to this branch.  If so, try

    cvs checkout -r string_methods python

General information on the Python CVS tree is available at

    http://www.python.org/download/cvs.html

Here's a brief summary of the changes...

Strings now have as methods most of the functions that were previously 
only in the string module.  If you've played with JPython, you've
already had this feature for a while.  So you can do:

Python 1.5.2+ (#1, Jun 10 1999, 18:22:14)  [GCC 2.8.1] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> s = 'Hello There Devheads'
>>> s.lower()
'hello there devheads'
>>> s.upper()
'HELLO THERE DEVHEADS'
>>> s.split()
['Hello', 'There', 'Devheads']
>>> 'hello'.upper()
'HELLO'

that sort of thing.  Not all of the string.py functions have been
added as methods, although most of the missing ones (e.g. ljust,
rjust, center, zfill, expandtabs, capwords) will probably be added at
some point.

Two new methods startswith and endswith act like their Java cousins.

join has interesting new semantics thanks to Tim Peters.  Use it as a
method on the separator string:

>>> '-!-'.join(['hello', 'string', 'SIG' ])
'hello-!-string-!-SIG'

which reads naturally if you assign the separator to a meaningful
variable:

>>> space = ' '
>>> empty = ''
>>> dash = '-'
>>> space.join(['that', 'seems', 'natural'])
'that seems natural'
>>> empty.join(['that', 'seems', 'natural'])
'thatseemsnatural'
>>> dash.join(['that', 'seems', 'natural'])
'that-seems-natural'

The string module has been rewritten to be backwards compatible.  No
code should break, though some small performance degradation is
possible.  Guido and I decided that was acceptable.

What else?  Some cleaning up of the internals based on Greg's
suggestions.  A couple of new C API additions.  Builtin int(), long(),
and float() have grown a few new features.  I believe they are
essentially interchangable with string.atoi(), string.atol(), and
string.float() now.

Still to do:

- add the string.py functions that never had a C implementation

- synchronize CPython and JPython

- document this stuff

- integrate Unicode (waiting to see what /F and MarkH come up with)

Enjoy,
-Barry