[Python-Dev] Re: other "magic strings" issues

Phillip J. Eby pje at telecommunity.com
Mon Nov 10 16:31:47 EST 2003


At 10:12 PM 11/10/03 +0100, Marangozov, Vladimir (Vladimir) wrote:

>Put it another way, it's good to have all string functions being
>attributes to a single well-known object, that object being the
>'string' module, instead of spreading it all over...  So add the
>attributes if you wish so (I respect OO minds), but don't zap
>the module (i.e. please respect mine ;-).

Actually, even in Python 2.2, you can access the same functions as 
'str.whatever', e.g.:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
 >>> str.upper("foo")
'FOO'
 >>> str.join(" ",["1","2","3"])
'1 2 3'
 >>> str.split("x y z")
['x', 'y', 'z']
 >>> str.count("a+b+c","+")
2

In fact, the only items missing from 'str' as opposed to 'string' in 2.2 are:

Constants
---------
ascii_letters
ascii_lowercase
ascii_uppercase
digits
hexdigits
letters
lowercase
octdigits
printable
punctuation
uppercase
whitespace


Functions and Exceptions
------------------------
capwords  (actually, the same as str.title)
joinfields  (alias for join, so str.join really suffices)
index_error
maketrans
atof, atof_error
atoi, atoi_error
atol, atol_error


So, the actual discussion is mostly about what to do with the constants, as 
the functions are already pretty much available in 'str'.  Note that since 
'str' is a built-in, it doesn't have to be imported, and it's three less 
characters to type.  So, if you prefer a non-object style for strings, you 
could still do it if string went away.  For legacy code support, you could 
probably even do:

sys.modules['string'] = str

in some cases.  :)




More information about the Python-Dev mailing list