Update for all the various ways of checking for a string/unicode object

in 2.3, 2.2, 2.1/2.0.
This commit is contained in:
Neal Norwitz 2002-06-04 16:57:44 +00:00
parent 3d6502df3f
commit 22cf1a49f9
1 changed files with 14 additions and 3 deletions

View File

@ -540,12 +540,23 @@ Programming Recommendations
Yes: if isinstance(obj, int):
When checking if an object is a string, keep in mind that it
might be a unicode string too! In Python 2.2, the types module
has the StringTypes type defined for that purpose, e.g.:
might be a unicode string too! In Python 2.3, str and unicode
have a common base class, basestring, so you can do:
from types import StringTypes:
if isinstance(strorunicodeobj, basestring):
In Python 2.2, the types module has the StringTypes type defined
for that purpose, e.g.:
from string import StringTypes
if isinstance(strorunicodeobj, StringTypes):
In Python 2.0 and 2.1, you should do:
from string import StringType, UnicodeType
if isinstance(strorunicodeobj, StringType) or \
isinstance(strorunicodeobj, UnicodeType) :
- For sequences, (strings, lists, tuples), use the fact that empty
sequences are false, so "if not seq" or "if seq" is preferable
to "if len(seq)" or "if not len(seq)".