[Zope] Add new property to a class

Dylan Reinhardt zope@dylanreinhardt.com
Sun, 02 Mar 2003 16:10:28 -0800


At 01:17 PM 3/1/2003, Luis Machado wrote:
>Is there a way to programatically modify the existing instances and add
>this new property to them.

Sure.  You could create an update method that adds a new value and call 
that method on all existing instances.

You can also accomplish this with class variables.  These will get loaded 
by existing instances and then any work you do on them will override them 
at the instance level.  Ex:

class foo:
    __init__(self):
       self.var1 = 'foo'
       # oops!  we forgot to assign var2

    var2 = 'bar'    # this is a class variable, added after some instances 
have been created

    def some_method(self):
       # now we can use var2, since we get it through the class
       # once we assign it a value, it's an instance variable for this instance
       self.var2 = 'spam'


HTH,

Dylan