[Python-3000] Making strings non-iterable
Ian Bicking
ianb at colorstudy.com
Mon Apr 17 06:37:51 CEST 2006
Aahz wrote:
> On Sun, Apr 16, 2006, Tim Peters wrote:
>> Now that I think of it, though, I've been burned perhaps twice in my
>> total Python life by recursing on a string when I didn't intend to.
>> Apart from flatten()-ish functions, I'm not sure I've written anything
>> vulnerable to that.
>
> I've been burned a few times, but only because I was expecting a list
> input and got a string. I think that improving code analysis tools and
> making better use of unit tests is the proper cure for that.
The problem with testing is that there's no exception, just bad data,
and so you have to test much more aggressively to find the bug. Also,
when you encounter the bad it's often not clear where exactly it came from.
Unit testing works best when things fail early and fail with an
exception, so I don't think this is a case where we should lean on unit
testing. Also, this leads to integration bugs -- when someone calls
your code with the wrong kind of argument. For any public API that
takes a list of strings, I always try to do "assert not isinstance(arg,
basestring)", because a runtime assertion is the only kind of test you
can do -- it's outside the bounds of unit testing.
--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org
More information about the Python-3000
mailing list