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

Tim Peters tim.one at comcast.net
Thu Jan 29 12:52:50 EST 2004


[Aahz]

I'm curious: why you didn't use None as the initial value or use some other hack to avoid initializing with a specific number?

In 2.3, None compares less than anything else; the search loop required that the initial value compare larger than anything else (although I wouldn't have been comfortable with relying on that None compares smaller either, since that's non-obvious version-specific behavior).

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

but it's easy to forget the "global_min is None" clause -- in which case, because None compares less than everything else, the "if" test never passes.

A conventional spelling for universal smallest and largest objects would allow for a very clean and clear solution. I'm reminded that we introduced "print >> f" partly because most people made mistakes in details when trying to temporarily rebind sys.stdout "by hand". Complex search loops are brittle in the details too. So I'll let Barry propose that a bare ">>" mean "biggest" and a bare "<<" mean "smallest" .

<< < >> True



More information about the Python-Dev mailing list