[Zope3-dev] interface conventions: default=_RAISE_ERROR

Casey Duncan casey@zope.com
Tue, 14 May 2002 15:03:16 -0400


Maybe handling the default should be the applications responsibility. Exp=
licit=20
if you will:

no_value =3D object()
value =3D someob.get(key, no_value)
if value is no_value:
    ...We got nothin'...

A bit more verbose, but pretty clear I think, no?

-Casey

On Tuesday 14 May 2002 02:32 pm, Jim Fulton wrote:
> Guido van Rossum wrote:
> >=20
> > > > in the __init__ of the Interfaces package.  An interface that wan=
ts to=20
use
> > > > the default marker would simply
> > > > from Interface import Interface, _RAISE_ERROR
> > > > and implementations could import from their interfaces or from th=
e=20
original
> > > > declaration in the Interface package.
> > >
> > > I really wouldn't bother. It saves very little typing, adds to the
> > > complexity of finding out what things really mean, adds another
> > > dependency, and saves a few hundred bytes at most per runtime.
> >=20
> > While we're at it, I don't particularly like the pattern.=20
>=20
> The pattern is based on Python's getattr. :)
>=20
> The pattern was extended in Zope 2 by my confusion over the inconsisten=
cy
> between getattr and (dictionary) get.  I assumed that they would be=20
consistent
> and all of Zope 2's get methods raise an error is a value is not found =
and
> no default is given.
>=20
> I'd be OK with treating getattr as an aberation. :)
>=20
> There is the problem that it is sometimes difficult for the caller
> of get to detect the not-found case. It's usually as hard for a client =
to
> pick a marker to pass to get to detect the non-found case when it needs
> to.
>=20
> > 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.
>=20
> I like this. Note, however, that it would be good enough to not raise
> an exception but to get this value when a key is not found.
>=20
> > 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?
>=20
> This feels too brittle to me.
>=20
> Maybe we should switch to having two versions of each get method.
> This would be easier to implement and to document.  Anybody got
> a suggestion for a naming convention?
>=20
> Jim
>=20
> P.S. I'm really happy that this kind of dialog is taking place and thea=
t=20
we're
>      taking the time, even at the expense of slowing Zope 3's developme=
nt,=20
>      to make things clearer.
>=20
> --
> Jim Fulton           mailto:jim@zope.com       Python Powered!       =20
> CTO                  (888) 344-4332            http://www.python.org =20
> Zope Corporation     http://www.zope.com       http://www.zope.org
>=20
>=20
> _______________________________________________
> Zope3-dev mailing list
> Zope3-dev@zope.org
> http://lists.zope.org/mailman/listinfo/zope3-dev
>=20