[Grok-dev] model not getting persisted

Kevin Teague kevin at bud.ca
Fri Mar 23 18:07:49 EDT 2007


>
> Ok, I understand this. But how is the zope3 code working then? I've  
> seen
> this
>
> class X(Persistent):
>    implements(ISomeSchema)
>
>    a = u''
>    b = u''
>
> a couple of times. How come the class attributes gets persisted here?
>

Those class attributes do not get persisted. They are just there as  
defaults, so that you don't need to initialize them in the  
constructor. With a class like the one above you can then write code  
like this:

 >>> obj = X()
 >>> obj.a # instance attribute does not yet exist, so falls back to  
class attribute
u''
 >>> obj.a = u'Hello ZODB' # sets an instance attribute
 >>> obj.a
u'Hello ZODB'
 >>> obj.__class__.a # class attribute is still available, it's now  
just 'shadowed' by an instance attribute
u''

This bit of documentation explains class vs instance attributes well:

http://www2.lib.uchicago.edu/~keith//courses/python/class/5/#classinst


More information about the Grok-dev mailing list