[Zope-dev] Re: [Zope-Coders] Re: Question about procedures

Florent Guillaume fg at nuxeo.com
Tue Mar 29 10:21:03 EST 2005


martin f krafft  <madduck at madduck.net> wrote:
> also sprach Florent Guillaume <fg at nuxeo.com> [2005.03.24.1814 +0100]:
> > > -        if RESPONSE is not None:
> > > +        if RESPONSE is not None and ob:
> > 
> > You should check 'and ob is not None' too.
> 
> ... but ob is false when it is None, no?

Yes but comparing to None is faster, and in some cases (REQUEST for
instance), much much faster, than checking the boolean value.

> > But why could it be None ? What's the point (sorry I don't have
> > context).
> 
> Well, I was trying to guard against errors made in other parts of
> the code. I know I should not do this. It made things a lot easier
> while I was preparing some other patches. Anyway, good thing
> I haven't committed. :)
> 
> > > +            if not hasattr(ob, 'absolute_url'):
> > 
> > Do not use hasattr for persistent objects. Use
> >                if getattr(ob, 'absolute_url', None) is None:
> 
> Can I read up on the rationale somewhere?

It was discussed at length on the zope lists. Basically hasattr tries to
access the attribute and returns false if *any* exception is raised.
This hides exceptions, which is bad in the case of persistent objects
that can raise ConflictErrors.

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