[Grok-dev] Re: model not getting persisted
faassen at startifact.com
Fri Mar 23 18:04:33 EDT 2007
Tim Terlegård wrote:
> Ok, I understand this. But how is the zope3 code working then? I've seen
> class X(Persistent):
> a = u''
> b = u''
> a couple of times. How come the class attributes gets persisted here?
They are not persisted, just like methods are not persisted and still
work. These attributes exist in RAM, and since they don't change, there
is no problem.
Of course you may *think* they change when you do:
self.a = u'something else'
somewhere, but in fact they don't change. That that creates is a new
*instance* attribute 'a' that shadows the 'a' on the class. This
behavior allows one to put in defaults for attributes.
My recommendation is to use class attributes with care. Most of the
time, just initialize things in __init__ and don't use class attributes
in Python code. It's not I want to ban class attributes - they're useful
in moderation, but typically you'd want to avoid them if you already
have an instance attribute with the same name (this is good Python style
that goes beyond the ZODB in my opinion). Definitely avoid using
mutables (such as lists) as class attributes, as changing those will
*not* create a new name to shield the one on the class, of course.
More information about the Grok-dev