[Zope-dev] getUserById

Florent Guillaume fg at nuxeo.com
Tue Dec 14 06:37:41 EST 2004


Jens wrote:
> On Dec 13, 2004, at 17:15, Florent Guillaume wrote:
> > Can we have instead:
> >
> >     def getUserById(self, id, default=_marker):
> >         """Return the user corresponding to the given id.
> >
> >         Raises a KeyError if the user does not exist and no default
> >         is provided.
> >         """
> >         user = self.getUser(id)
> >         if user is not None:
> >             return user
> >         if default is not _marker:
> >             return default
> >         raise KeyError(id)
> 
> What is the advantage of throwing a KeyError over simply returning 
> None?

Well it's standard usage all over Zope for all methods that accept an
optional default to raise an error if no default is provided. Why accept
a default when None is returned anyway if no default is provided, and
None is not an acceptable user in itself ? That just doesn't make sense.

> I don't see the reason for "getUser" and "getUserById" behaving 
> in different ways here. They should both behave similar.

Chris clarified that getUser is really getUserByName so I'm ok with
that. If the method should be never raise and return None if nothing's
provided, then let's use simply:

def getUserById(self, id, default=None):
    user = self.getUser(id)
    if user is not None:
        return user
    return default

or even:

def getUserById(self, id, default=None):
    return self.getUser(id) or default

Florent

-- 
Florent Guillaume, Nuxeo (Paris, France)   CTO, Director of R&D
+33 1 40 33 71 59   http://nuxeo.com   fg at nuxeo.com


More information about the Zope-Dev mailing list