[Zope3-dev] interface conventions: default=_RAISE_ERROR

Barry A. Warsaw barry@zope.com
Wed, 15 May 2002 01:36:35 -0400


>>>>> "JF" == Jim Fulton <jim@zope.com> writes:

    JF> The pattern is based on Python's getattr. :)

    JF> I'd be OK with treating getattr as an aberation. :)

Yes, please!  I've always found that getattr's behavior is jarring,
probably because I use dict.get() much more often than getattr.  I'm
always forgetting to tack the None argument in on the getattr call. ;)

    JF> There is the problem that it is sometimes difficult for the
    JF> caller of get to detect the not-found case. It's usually as
    JF> hard for a client to pick a marker to pass to get to detect
    JF> the non-found case when it needs to.

Not really.  I found this to be a common idiom when I was implementing
the email package -- which uses dict.get() as a model where
appropriate:

    def get(self, name, failobj=None):
	# ...

    def has_key(self, name):
	missing = []
	return self.get(name, missing) is not missing

It even reads nicely!

Using "missing = object()" for Python 2.2-only code is probably fine,
maybe better (criteria?  speed?  memory footprint?  I dunno).  Just
don't use "missing=()". :)

    JF> Maybe we should switch to having two versions of each get
    JF> method.  This would be easier to implement and to document.
    JF> Anybody got a suggestion for a naming convention?

Yeah, get_ex() except that Python's tradition seems to be that the
ex() version doesn't raise an exception when the non-ex() version
does.  Witness socket object's connect_ex() method.  (Go figure;
shouldn't something that ends in "ex" probably raise the exception
instead of the other way 'round? :).

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.

-Barry