[Zope3-dev] interface conventions: default=_RAISE_ERROR

Martijn Faassen faassen@vet.uu.nl
Sat, 11 May 2002 22:04:01 +0200


Gary Poster wrote:
> On Friday 10 May 2002 08:26 pm, Guido van Rossum wrote:
[special default to signal raising of exception]
> > While we're at it, I don't particularly like the pattern.  If it is
> > used I think there should be a *public* value so that a caller can be
> > explicit about "I want the exception" rather than having to use a
> > short argument list.
> >
> > Where this is used, would it be acceptable if the exception itself
> > (KeyError?) was specified as the default value?  Or is an exception a
> > valid regular default?

> Yup, better solution.  As far as I'm concerned, at least.  Can I start using 
> this, folks?

What's the reason for the existence of the pattern in the first place,
though? Why do you *want* to use it? I mean, Guido raised a legitimate
issue there in my mind; having to handle exception raising defaults makes
the contract of an interface harder to fulfill, and I don't see much
of a benefit. 

_RAISE_KEYERROR right now only seems to be used in Containers and in
the traversal interface. We were talking about changing the container
interface to be more like a Python mapping, in that you can do
__getitem__() access and the like. KeyError would then presumably
be raised if an item cannot be found, and the default pattern is used
with the get() method.

You could argue the complexity of the latter interface (get and getitem) is
the same as the former, but I disagree; in the former case you need a single
method to behave differently (presumably with an 'if' or other conditional
pattern somewhere inside), which is harder to forget while you implement
the method (as it seems to work) and harder to deal with if you unthinkingly
use the method. Plus it's also unlike Python where the default is None.
In constrast, if you're going to implement get() on top of __getitem__ it's 
pretty hard to do it wrong.

Regards,

Martijn