The worth of comments

Chris Angelico rosuav at gmail.com
Sat May 28 10:02:57 EDT 2011


On Sat, May 28, 2011 at 11:36 PM, Roy Smith <roy at panix.com> wrote:
> def foo():
>   "Raise IndexError.  This is useful as a testing fixture."
>   l = [1, 2, 3]
>   return l[3]

A quite useful thing, on occasion. I have a couple of variants of
this, actually. In one of my C++ programs:

extern char *death1; extern int death2; //Globals for killing things with

// further down, inside a function:
case "death1": *death1=42; break; //Die by dereferencing NULL
case "death2": return 42/death2; //Die by dividing by zero

They were designed to verify the parent-process code that was meant to
catch process termination and identify the cause, so I wanted two
quite different ways of blowing up the program. (The variables were
extern and defined in another file to ensure that the compiler
couldn't outsmart me with a compilation error.)

In the Python code, that would be unnecessary with the *list* type,
but it might be of value with your own class (eg subclass of list).
Although, I'd put that sort of thing into a dedicated unit testing
section, where everyone _knows_ that you're trying to break stuff.

Chris Angelico



More information about the Python-list mailing list