[Zope3-dev] class lifecycle management/missing attributes
Guido van Rossum
guido@python.org
Tue, 01 Jul 2003 20:59:39 -0400
> Is there an example in the Zope 3 code that illustrates a solution to
> the following problem:
>
> 1 - A component of class Foo is created in the ZODB.
>
> 2 - Class Foo is modified, adding a new attribute in __init__:
>
> def __init__(self):
> self.bar = 'some_default'
>
> 3 - The component in the ZODB doesn't have bar -- bad.
>
> How would one add bar to instances of Foo already serialized? In other
> apps, I've handled this by adding the missing attribute in a deserialize
> method.
>
> I assume there's a pattern for this, but haven't spotted anything in the
> source.
If all you want is a default with an immutable value, you can put it
in the class instead of in __init__.
Otherwise, overriding __setstate__ would be an option, but I don't
know if it's the best practice.
--Guido van Rossum (home page: http://www.python.org/~guido/)