[Zope3-dev] Services in boolean context

Marius Gedminas mgedmin@codeworks.lt
Sat, 7 Dec 2002 21:13:23 +0200


Yesterday we noticed a curious phenomenon: a placeful service was
created and configured correctly, yet getService('...') could not find
it.  The cause turned out to be an if statement like this in getService:

  if service:
      ...

Our service inherited from a BTreeFolder, which has a __len__ method.
When the folder is empty, __len__ returns 0, and that if statement
fails.

Should getService be fixed to use 'if service is not None:'?  I think
not; the short form is so much more intuitive that someone is bound to
use it after calling, say, queryService() somewhere.

I think the convention should be 'services ought to evaluate to True in
boolean context', e.g. by defining __nonzero__ when they have a __len__.

At the moment there are at least two services that inherit from
BTreeFolder -- CachingService and ConnectionService.  AFAIU the plan is
to change their architecture so that they are no longer folders, and
this question will no longer be relevant to them.

There's a related question -- should empty folders evaluate to False?
I haven't given it much thought yet.  Empty containers are False,
though, so it makes sense at a glance.

Marius Gedminas
-- 
Everyone has a photographic memory. Some don't have film.