[ZODB-Dev] Is any function called when an object is loaded from the database?

Vincent Pelletier plr.vincent at gmail.com
Tue Jun 19 20:15:03 UTC 2012


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 ?

-- 
Vincent Pelletier


More information about the ZODB-Dev mailing list