Previous Chapter | Up | Contents

Subclassing from Custom Python Classes


One very useful technique is to create a Python class which is subclassable by a Z Class. This way you can write your application logic in Python, and your user interface in Zope!

To create a Python class which is subclassable by a Z Class you need to create a very simple Zope Product in Python. Your Product should consist of a Python package installed inside the lib/python/Products directory. In the package's __init__.py file you need to register your Python class as subclassable by a Z Class.

To accomplish this simply create an initialize function in the package's __init__.py file like this:

import MyModule

 

def initialize(context):

"""

This function is called by Zope to initialize a Product. See

lib/python/App/ProductContext for more information about

Product initialization.

"""

context.registerBaseClass(MyModule.MyClass)

Now your Python class is ready to be used as a Z Class subclass. You should note that your subclass's __init__ method will not be called when instances of the Z Class are created. You should also note that your subclass must obey the ZODB persistence rules, which basically state that mutable sub-objects should be treated immutably. For example,

self.bird='parrot' # OK

 

self.map['bird']='parrot' # NOT OK, will not trigger persistence machinery

 

m=self.map

m['bird']='parrot'

self.map=m # OK, treats a mutable object immutably

 

You should probably regard this kind of subclass as a sort of mix-in class. It's probably best to leave most of the Zope framework-specific methods to the Z Class and to concentrate on application logic in your Python class.

 

 

A
B
C
D
F
I
M
O
P
S
U
V
W
Z
Previous Chapter | Up | Contents

Banner.Novgorod.Ru