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

Tim Peters tim.one@comcast.net
Tue, 22 Apr 2003 00:06:10 -0400


[Jeremy Fincher]
> This code brought up an interesting question to me: if sets have
> a .discard method that removes an element without raising KeyError
> if the element isn't in the set, should lists perhaps have that same
> method?

I don't think list.remove(x) is used enough to care, when the presence of x
in the list is unknown.  Adding methods for purity alone is neither Pythonic
nor Perlish <wink>.

> 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?

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).