On Tue, Jun 19, 2012 at 10:15:03PM +0200, Vincent Pelletier wrote:
> Le mardi 19 juin 2012 19:54:21, Claudiu Saftoiu a écrit :
> >         def calc_it(self, b):
> >             if not hasattr(self, 'b_cache'): self.b_cache =
> > PersistentDict()
> 
> If you were to use this, you would probably prefer to do
>     try:
>         self.b_cache
>     except AttributeError:
>         self.b_cache = PersistentDict()
> (assigning the result to a vairable, etc)
> as this is faster when b_cache is actually here (which is much more likely 
> than not). Also, it avoids hiding too many exceptions, as with hasattr. In 
> the 
> times of ReadConflictError it was quite important (at least to stop 
> processing 
> earlier on a doomed transaction). I'm not sure how important this is nowadays 
> - I just kept the habit of avoiding "hasattr" on persistent objects.
> 
> > The former is OK, but if I have multiple functions that want to use the new
> > functionality I'll have to have the code all over, and it won't be obviously
> > separated.
> 
> Maybe a @property taking care of creating the actual PersistentDict if not 
> present, then ?

zope.cachedescriptors.property.Lazy would work even better:


    @Lazy
    def b_cache(self):
        return PersistentDict()


Marius Gedminas
-- 
If you have to run heating in winter, you don't own enough computers.

Attachment: signature.asc
Description: Digital signature

_______________________________________________
For more information about ZODB, see http://zodb.org/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to