[Zope-dev] Using default values for variables in class definitions ... Is this o.k.?
Leonardo Rochael Almeida
leo@hiper.com.br
Tue, 06 Nov 2001 10:45:33 -0200
Joachim Werner wrote:
> Hi!
Hello,
>
> I am facing the following common problem:
>
> Let's say I have a filesystem-based Python Zope Product that uses
> propertysheets. I have defined a couple of properties and already have a
> number of instances of the class.
>
> Now I add a new property to the code. While this would not break anything if
> I worked with ZClasses, the filesystem-based propertysheet implementation
> will raise an error if I visit my existing instances' Properties tag because
> the property is not there.
>
> To avoid that, I have used the trick to add a default value for the new
> property in the class definition:
>
> class News(...):
>
> meta_type = 'News'
> ...
> myNewProperty='Green'
> ...
>
> def ...
>
> etc.
>
> This works perfectly, but I have been told that I should not do this because
> of the way ExtensionClasses work. Does anybody know more about that?
Well, not only it works, but it is actually the recomended way to do it.
This is from comments at the start of lib/python/OFS/PropertyManager.py
The _properties structure is a sequence of dictionaries, where
each dictionary represents a predefined property. Note that if a
predefined property is defined in the _properties structure, you
must provide an attribute with that name in your class or instance
that contains the default value of the predefined property.
Cheers, Leo