[Zope-dev] Advanced ZClass Creation Question

James W. Howe jwh@allencreek.com
Thu, 09 Mar 2000 17:09:16 -0500


I'm building a product with a handful of ZClasses.  I've built a couple of 
them and I find that I'm duplicating method code everytime I create a new 
ZClass definition.  What I would like to do is abstract out some of the 
commonality into a base Python class that all my ZClasses would derive 
from.  However, my brain starts to hurt when I think about the difference 
between sending messages to the ZClass object itself (say 
createInObjectManager), and sending messages to the instance which gets 
created by that creation method.  What I want to be able to do is have a 
common object creation method as well as some common instance 
methods.  Would these all be defined in one base class?

For example, right now when I create an instance of my ZClass, I compute a 
unique ID and then pass that value into the createInObjectManager 
method.  To do this, I'm thinking I would create a class like this:

class MyCommonZClassBase:
    def createNewObject(self, REQUEST=None):
         newid = self.computeNewId()
         return self.createInObjectManager(newid, REQUEST)

When I defined my ZClass, I would include "MyCommonZClassBase" as a base 
class of my ZClass (actually I would probably create a custom Python class 
for each ZClass that I wanted to create and this class would derive from 
MyCommonZClassBase).

I assume that I could then create a new object by doing:

MyZClass.createNewObject(REQUEST)

Now that I've created my new object, I also have some things that I want to 
do whenever any of my ZClasses gets instantiated.  For example, I want to 
set properties and update a Catalog (with an arbitrary name)

To do this, I would might write a method like this:

def initNewObject(self, REQUEST):
      REQUEST.set('title', REQUEST['name'])
      self.unindex_object()
      self.manage_editCataloger(self.catalogName, REQUEST)
      self.index_object()

My question (and confusion) is this, would I define this method in 
MyCommonZClassBase, or would it have to be defined somewhere else?  In 
Smalltalk, this would be handled by having a class method called 
"createNewObject" and an instance method called "initNewObject".  Python 
doesn't seem to have that distinction, so I'm confused as to what I would 
need to do to make this work.

Any tips or insights on this matter would be greatly appreciated.

Thanks.

James W. Howe				mailto:jwh@allencreek.com
Allen Creek Software, Inc.		pgpkey: http://ic.net/~jwh/pgpkey.html		
Ann Arbor, MI  48103