[Zope-CMF] Add attributes to class which already has instances
in ZODB
Chris Withers
chrisw@nipltd.com
Fri, 10 Jan 2003 18:26:48 +0000
Jeffrey P Shell wrote:
> If you're writing the class in Python, you can add a __setstate__ method
> to the class. This is called when the ZODB loads an instance of your
> class into memory.
This is pretty brittle though.
> Another way is to put the attribute definitions inside the class block
> itself. Due to Python's laws of mutability, you have to be careful what
> you put in here. But putting an attribute in a class statement would
> cause all objects to pick up that attribute (they're really "Shared
> Instance" attributes).
>
> class Foo(Persistent):
> email = ""
Yep, where the added attributes aren't mutable types, this is often theeasiest
solution.
> Of course, a better design would be to implement everything with
> getter/setter methods (or at least getters). It makes changes to the
> class easier.
Yeah, yeah ;-)
> This design pattern is best for Zope because you can keep the actual
> attribute hidden from Python Scripts, etc (they can't access names with
> underscores), and place different permissions on the 'email' and
> 'set_email' methods. You can also avoid acquisition surprises, since
> you can't acquire names beginning with an underscore.
...these are good reasons though.
Another option is to write an external method that goes through an updates all
existing instances of the class that are missing the attributes.
I wrote http://www.zope.org/Members/chrisw/UpdateSupport to help with this, and
Squishdot (http://squishdot.org/Documentation/upgrades.html) has used it with
great success for ages.
cheers,
Chris