[Zope3-dev] interface conventions: default=_RAISE_ERROR

Guido van Rossum guido@python.org
Wed, 15 May 2002 09:38:36 -0400


> FWIW, I generally feel that getattr() is broken (but obviously can't
> be changed) and that accessors generally shouldn't raise an
> exception.  dict.get() is the right and convenient model.  I
> definitely think that the semantics of whether an exception is
> raised or not should never be decided on the basis of an argument.
> I've rarely felt the need, but if you think we must have both
> varieties, please make them different methods.

I think you're going too far here.  While it's sometimes inconvenient
that getattr() raises an exception (I think that maybe 50% of the
occurrences in the standard library are surrounded by a try/except),
the customary default of None really doesn't work for getattr(),
because it's a legal value.  dict.get() is different because it is
specifically intended to provide a default, and the dict[key] notation
is available for when the exception is okay.

What's wrong with getattr() is that it raises an exception with 2
arguments but returns a default with 3 arguments, and that's the
pattern we're trying to stomp out here -- not the idea that accessors
can raise exceptions.

--Guido van Rossum (home page: http://www.python.org/~guido/)