[ZODB-Dev] Is any function called when an object is loaded from the database?
Marius Gedminas
marius at gedmin.as
Tue Jun 19 21:21:30 UTC 2012
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://mail.zope.org/pipermail/zodb-dev/attachments/20120620/b6e7540d/attachment.sig>
More information about the ZODB-Dev
mailing list