[Zope-dev] ZClasses instances become detached from class

Brian Lloyd Brian@digicool.com
Mon, 12 Jul 1999 10:02:21 -0400


> This distinction between class and instance is significant. One caveat
> concerns folders: Don't make the mistake of creating a folder within
> your class. If you do that, each instance will inherit this folder, as
> expected. But any modifications to perform on the folder -- including
> adding objects -- will show up in all instances. (Of course, 
> there might
> be cases where this is exactly the intended effect.) So in 
> this case you
> should not create the folder within the class, but let one of your
> methods create it.
> 
> (Imho, this is a weakness in the current ZClass model: You can't add
> constructors. So given the previous paragraph, there's no way you can
> say, "When an instance of this class is created, such and such folder
> should be created within the instance.")

Well, technically there is a constructor of sorts - the DTML method
or External method that is the target of the "Add" form for your 
ZClass is basically the equivalent of a Python constructor, except
that it is outside-in, rather than inside-out. Where in Python you
might do:

  def __init__(self, id):
    self.id=id
    self.sub=self.CreateNewFolder('blah')

...in the target of your Zope Add form for the ZClass you would
do something like this if you were using a DTML Method:

  <!-- Create an instance of our class in the target object -->
  <!--#with "MyZClass.createInObjectManager(REQUEST['id'], REQUEST)"-->

    <!-- Set properties based on the contents of the Add form -->
    <!--#call "propertysheets.Basic.manage_changeProperties(
               REQUEST)"-->

    <!-- Create a new Folder in the new MyZClass instance -->
    <!--#call "manage_addFolder('foobar')"-->

  <!--#/with-->


...or in an external method you might do:

  def addAMyZClass(self, id, REQUEST=None):
    """ """
    # Get the actual destination object, using the this()
    # method, to be sure we get it in the right context..
    self=self.this()

    # Create the new instance
    newob=self.MyZClass(id)
    newob.id=id

    # Set properties based on the contents of the Add form
    newob.propertysheets.Basic.manage_changeProperties(REQUEST)

    # Add a new Folder to the new instance
    newob.manage_addFolder('foobar')

    # Add the new instance to the target object
    self._setObject(id, newob)

    return 'OK!'


Hope this helps!


Brian Lloyd        brian@digicool.com
Software Engineer  540.371.6909              
Digital Creations  http://www.digicool.com