[Python-Dev] PEP 326 (quick location possibility)

Andrew P. Lentvorski, Jr. bsder at allcaps.org
Thu Jan 29 19:46:30 EST 2004


On Thu, 29 Jan 2004, Aahz wrote:

> > I've since tended to write these kinds of loops as:
> >
> >     global_min = None
> >     ...
> >
> >         if global_min is None or score(candidate) < global_min:
> >             global_min = score(candidate)
> >             do stuff appropriate for a new local minimum
>
> That's precisely what I was suggesting, yes.

The code is still brittle.  If score(candidate) returns None, hilarity
ensues.

None happens to compare less than everything; consequently, that None
slides into global_min and obliterates the previous results.  The next
value wipes out the None because None is the initial sentinel.
global_min now has a value which isn't minimal, but the error is hidden.
The errors may even be random depending upon whether the real minimum
comes before or after the None gets returned.  (Gee, do ya think I've seen
this before?)

Even worse, None is the most likely value to appear in case of a bug since
it is the default return value for a function which doesn't explicitly
return a value.

If magnitude comparisons involving None raised some kind of Exception,
this would not be an issue.

-a



More information about the Python-Dev mailing list