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:
parent
3d6502df3f
commit
22cf1a49f9
17
pep-0008.txt
17
pep-0008.txt
|
@ -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)".
|
||||
|
|
Loading…
Reference in New Issue