[Zope] Altering product instance changes all instances!

Michel Pelletier michel@digicool.com
Mon, 01 Nov 1999 11:52:20 -0500


Edd Dumbill wrote:
> 
> Hi
> 
> For my SiteSummary [1] product, I have a default image that is stored as a
> subobject in my ZClass.
> 
> An instance of the product can upload a new image to replace it.
> 
> Unfortunately that ends up replacing the image in _all_ instances of the
> product.
> 
> This is not the desired behavior!

But it is probably the correcy behavior.  What you've done is replace a
*class* attribute, not an instance attribute when you upload the new
image.  The instances of your product behave right, they replace the
attribute with the new value, it's just that that attribute is a class
attribute (because you defined it in the ZClass) and thus, all instances
share it.  In terms of Python:

class AZClass:
  class_attr = image              <= this is what your doing

  def __init__(self):
    self.instance_attr = image    <= this is what you want to do

It sounds like maybe you want your object to be an ObjectManager, and
contain your Image.  I suppose the 'right' thing would be for each
instance to set an instance attribute when they are instanciated, but
you can't do that, because you can't set attributes from DTML, it would
violate security.  You *can* do it from an external method, but that
might be hairy..hmm.

> Can anyone suggest what I'm doing wrong and how I might go about fixing
> this problem: ie. creating a distinct instance of the image for every
> product instance.

You might be stretching ZClasses farther than they are capable of going,
at this point.  It might be desireable for us to come up with some way
for instances to change their attributes, but this is typically not done
from DTML, DTML is expressly designed not to allow you to 'change'
objects without using a specific API that that object provides, in
Python, for the DTML programmer.

This is, I think, one of the big problems with why ZClasses are somewhat
'weak' (this is probably not the right word)... because they are
designed 'thru the web' they must be constrained under the usual Zope
secuirty concepts.  This means, unfortunatly, that you are restricted
then in what your class methods can do to the class unless you provide
the hooks in Python to do the meaty stuff, like assign instance
attribute (or call methods that begin with '_', etc.).

You *can* create a Python class that is subclassable by ZClasses.  This
PClass could define a method to let you change instance attributes.  It
would be ugly. ;)

-Michel

> 
> Thanks
> 
> Edd.
> 
> [1] http://www.zope.org/Members/edmundd/SiteSummary
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://www.zope.org/mailman/listinfo/zope
> 
> (Related lists - please, no cross posts or HTML encoding!
> 
> To receive general Zope announcements, see:
> http://www.zope.org/mailman/listinfo/zope-announce
> 
> For developer-specific issues, zope-dev@zope.org -
> http://www.zope.org/mailman/listinfo/zope-dev )