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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4