[Python-Dev] os.path.walk() lacks 'depth first' option

Tim Peters tim.one@comcast.net
Sun, 11 May 2003 22:38:25 -0400


[Jeremy Fincher]
>>> On another related front, sets (in my Python 2.3a2) raise KeyError on a
>>> .remove(elt) when elt isn't in the set.  Since sets aren't mappings,
>>> should that be a ValueError (like list raises) instead?

[Tim]
>> Since sets aren't sequences either, why should sets raise the
>> same exception lists raise?  It's up to the type to use whichever
>> fool exceptions it chooses.  This doesn't always make life easy for
>> users, alas -- there's not much consistency in exception behavior
>> across packages.  In this case, a user would be wise to avoid
>> expecting IndexError or KeyError, and catch their common base class
>> (LookupError) instead.  The distinction between IndexError and KeyError
>> isn't really useful (IMO; LookupError was injected as a base class
>> recently in Python's life).

[Michael Hudson]
> Without me noticing, too!  Well, I knew there was a lookup error that
> you get when failing to find a codec, but I didn't know IndexError and
> KeyError derived from it...
>
> Also note that Jeremy was suggesting *ValueError*, not IndexError...

Oops!  So he was -- I spaced out on that.

> that any kind of index-or-key-ing is going on is trivia of the
> implementation, surely?

Sure.  I don't care for ValueError in this context, though -- there's
nothing wrong with the value I'm testing for set membership, after all.  Of
course I never cared for ValueError on a failing list.remove() either.  I
like ValueError best when an input is of the right type but outside the
defined domain of a function, like math.sqrt(-1.0) or chr(500).  Failing to
find something feels more like a (possibly proper subclass of) LookupError
to me.  But I'd hate to create even more useless distinctions among
different kinds of lookup failures, so am vaguely happy reusing the KeyError
flavor of LookupError.

In any case, I'm not unhappy enough with it to do something about it.  I
nevertheless agree Jerry raised a good point, and maybe somebody else is
unhappy enough with it to change it?