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
    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 ?

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

ZODB-Dev mailing list  -  ZODB-Dev@zope.org

Reply via email to