Add new property to a class
Having defined a class using python like this one: from OFS import ObjectManager, SimpleItem class Plant(ObjectManager.ObjectManager, SimpleItem.SimpleItem): """A plant""" def __init__(self, name, responsible): "Inits the product with default values" self.name = name self.responsible = responsible self.areas = {} you create a few instances, and later you want to add more properties to the same class, for example: def __init__(self, name, responsible, address): "Inits the product with default values" self.name = name self.responsible = responsible self.areas = {} self.address = address Is there a way to programatically modify the existing instances and add this new property to them. Any help will be appreciated. Luis
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
participants (2)
-
Dylan Reinhardt -
Luis Machado