[Python-Dev] Re: String module

Guido van Rossum guido@python.org
Wed, 29 May 2002 14:13:20 -0400


> Instead of making direct substitutes for the character lists, I
> propose we take advantage of the opportunity and provide them as
> mappings rather than 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('_').

I don't like the idea of making this mutable at all.

But perhaps these should be replaced by predicates on strings?
Most of the constants already have a predicate companion:

whitespace -- isspace()
lowercase -- islower()
uppercase -- isupper()
letters -- isalpha()
digits -- isdigit()

That leaves:

hexdigits -- isxdigit()
octdigits -- isodigit()
punctuation -- ispunct()
printable -- isprint()

Perhaps we should add isalnum, iscntrl, is graph, to match <ctype.h>?
Or perhaps not?  (Maybe I'd also like to add isword(), which would be
isalnum() or '_' -- this is the definition of \w in the re module.)

--Guido van Rossum (home page: http://www.python.org/~guido/)