[Zope3-dev] RE: more on class/object lifecycles

Garrett Smith garrett@mojave-corp.com
Tue, 8 Jul 2003 15:12:04 -0500


Here are some answers Jim provided on this thread...

Jim Fulton wrote:
> Garrett Smith wrote:
>> To persist modified values of new attributes, don't they have to be
>> added to self? This is what I mean by 'object scope'.
>>=20
>> The 'cruft' was a reference to the "write on read" example Tres
>> provided in another message:=20
>>=20
>>      class FooContent(Persistent):
>>          _bar =3D None
>>          def __init__(self, baz):
>>              self._bar =3D Bar(baz)
>>          def bar(self):
>>              if self._bar is None:
>>                 self._bar =3D Bar() # write-on-read
>>              return self._bar
>>=20
>> I agree with him that this approach will degrade the clarity of code
>> over time.
>=20
> Right. If the attribute can't have a default value, as in the case
> above,. you need to use __setstate__, as in:
>=20
>       class FooContent(Persistent):
>=20
>           def __init__(self, baz):
>               self._bar =3D Bar(baz)
>=20
>           def bar(self):
>               return self._bar
>=20
> 	 def __setstate__(self, state):
>               Persistent.__setstate__(self, state)
>               if _bar not in state:
>                   self._bar =3D Bar()
>=20
>=20
> This isn't a "write on read", as it doesn't cause a
> database write,
>=20
> Using __setstate__ is acceptable in small amounts. If things
> get more complicated it's better to write a conversion script.
>=20
>=20
>=20
>> Is the general thinking to keep implement ZODB updates via external
>> tools (i.e. stuff run from the command line), rather than using
>> internal upgrade logic (i.e. something that could be registered in
>> ZCML as an internal upgrade handler)?
>=20
> There are a number of variations on this theme. *I* prefer this sort
> of upgrade to be explicit, so I prefer some explicit update step.
> I don't like implicitly doing write transactions when we see old data.
> There's a difference of opinion on this.