OKB (not okblacke) wrote: > John Machin wrote: > > > Can you give a real example from your code where the location of > > such a human error (you cause a variable to bound to a value of > > inappropriate type) would be difficult to find, plus an estimate of > > how often you would make such an error? > > Here is an example: > > self.outFile.write(str(len(self.hits)) + ' in ' + searchInfo.recordName > + '\t' + ']['.join((a. group() for a in self.hits)) + '\n') > > This is from a text-searching tool I have written to search > linguistic corpora. This statement writes a summary line to the output > file indicating how many hits were found in that file. > > The problem in this line was that I'd forgotten to put str() around > the len(). Now, it's not impossible to find the problem, because given > my knowledge of the program I know that that's the only part of the line > that would reasonably contain the number, but I still think it would be > a lot easier if there were a caret in the error message pointing to the > offending summand.
It should be extremely easy (rather than "not impossible") for anybody with half a clue to find the problem given only the offending statement (rather than your "knowledge of the program"). There are 6 possibilities; 3 are string constants. That leaves us with len(something), searchInfo.recordName, and "][".join(something). len() very definitely returns an integer (unless some lunatic has bound the name to something else) and it's the *first* of the possibilities -- one can normally expect execution to proceed from left to right. recordName smells like a string. "][".join(blahblah) likewise unless the rebinding mania is pandemic. Sheesh. > > I'm glad you asked this question, though, because in searching for > examples in my code, I discovered that most of my beefs aren't actually > of this type. A lot of them are things like this: > > someStr.split(someList) > > Here I meant to write someList[0] (say), but have inadvertently > passed the list instead of one of its elements. > > In this case I receive an error message that says "TypeError: > expected a character buffer object". My question is: why can this error > message not say what was encountered INSTEAD of a character buffer > object? > > A similar situation occurs when I do someVar[0] and get an > "unsubscriptable object" error. The error does not even tell me the > type of the offending value, just that it is unsubscriptable. > > So my conclusion from this is: is there a reason that every error > message of the form "expected foo" or "this object cannot be frotzed" > cannot be changed to something like "expected foo but found bar" or > "this FooType object cannot be frotzed"? And despite your use of RHN (Reverse Hungarian Notation) you don't know that someList is a list? -- http://mail.python.org/mailman/listinfo/python-list