Possible bug in string handling (with kludgy work-around)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Dec 26 20:10:19 EST 2011


On Mon, 26 Dec 2011 14:23:03 -0800, Charles Hixson wrote:

> This doesn't cause a crash, but rather incorrect results.

Charles, your code is badly formatted and virtually unreadable. You have 
four spaces between some tokens, lines are too long to fit in an email or 
News post without word-wrapping. It is a mess of unidiomatic code filled 
with repeated indexing and unnecessary backslash escapes.

You also don't tell us what result you expect, or what result you 
actually get. What is the intention of the code? What are you trying to 
do, and what happens instead?

The code as given doesn't run -- what's self?

Despite all these problems, I can see one obvious problem in your code: 
you test to see if self.wordList[i] is a string, and if not, you replace 
the *entire* wordList with the empty string. That is unlikely to do what 
you want, although I admit I'm guessing what you are trying to do (since 
you don't tell us).

Some hints for you:

(1) Python has two string delimiters, " and ' and you should use them 
both. Instead of hard-to-read backslash escapes, just swap delimiters:

print "A string including a \" quote mark."  # No!
print 'A string including a " quote mark.'  # Yes, much easier to read.

The only time you should backslash-escape a quotation mark is if you need 
to include both sorts in a single string:

print "Python has both single ' and double \" quotation marks."
print 'Python has both single \' and double " quotation marks.'


(2) Python is not Pascal, or whatever language you seem to be writing in 
the style of. You almost never should write for-loops like this:


for i in range(len(something)):
    print something[i]


Instead, you should just iterate over "something" directly:


for obj in something:
    print obj


If you also need the index, use the enumerate function:


for i,obj in enumerate(something):
    print obj, i


If you are forced to use an ancient version of Python without enumerate, 
do yourself a favour and write your loops like this:


for i in range(len(something)):
    obj = something[i]
    print obj, i


instead of repeatedly indexing the list over and over and over and over 
again, as you do in your own code. The use of a temporary variable makes 
the code much easier to read and understand.



-- 
Steven



More information about the Python-list mailing list