Greetings Zopistas!


I have been a Zope tinkerer for a few years and have never been able
to adequately solve this problem:  When I create a Product class with
DTML/ZPT methods, how can I make those methods members of the class
rather than simply instance methods?  These examples should illustrate
what I mean.


My first try was like this:

<fixed><color><param>0000,0000,FFFF</param>class</color>
HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item):

<color><param>0000,0000,FFFF</param>	def</color>
<color><param>0000,0000,FFFF</param>__init__</color>(self,resourcePath,
imageFolder):

</fixed> <fixed>		self.my_method =
Globals.HTMLFile(<color><param>5554,5554,5554</param>"dtml/my_method"</color>,globals())

</fixed>

That works fine until you want to add a new method to your class like
so:

<fixed><color><param>0000,0000,FFFE</param>class</color>
HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item):

<color><param>0000,0000,FFFE</param>	def</color>
<color><param>0000,0000,FFFE</param>__init__</color>(self,resourcePath,
imageFolder):

</fixed> <fixed>		self.my_method =
Globals.HTMLFile(<color><param>5552,5552,5552</param>"dtml/my_method"</color>,globals())

</fixed> <fixed>		self.my_new_method =
Globals.HTMLFile(<color><param>5552,5552,5552</param>"dtml/my_new_method"</color>,globals())

</fixed>

new instances of your product will get the my_new_method, but
instances that have already been created will not.


My second try was like this:

<fixed><color><param>0000,0000,FFFD</param>class</color>
HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item):

	<color><param>0000,0000,FFFF</param>def</color> my_method(self,
REQUEST):

		<color><param>5555,5555,5555</param>""" template """</color>

		<color><param>0000,0000,FFFF</param>if</color>
(<color><param>0000,0000,FFFF</param>not</color>
hasattr(self,<color><param>5555,5555,5555</param>"my_method_member"</color>)):

			self.my_method_member =
Globals.DTMLFile(<color><param>5555,5555,5555</param>"dtml/my_method"</color>,globals())

		<color><param>0000,0000,FFFF</param>return</color>
self.my_method_member(self,REQUEST)

</fixed>

This allows me to add methods to the class at any time and all
instances will get the method.  However, for some reason, when I use
this pattern, i get strange acquisition results (for example, some of
my objects children's dtml methods will use the parent's namespace for
lookups).


Currently, I'm doing this:

<fixed><color><param>0000,0000,FFFC</param>class</color>
HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item):

	<color><param>0000,0000,FFFE</param>def</color> my_method(self,
REQUEST):

		<color><param>5554,5554,5554</param>""" template """</color>

		self.my_method_member =
Globals.DTMLFile(<color><param>5554,5554,5554</param>"dtml/my_method"</color>,globals())

		<color><param>0000,0000,FFFE</param>return</color>
self.my_method_member(self,REQUEST)

</fixed>which seems ugly, and like it might have performance problems,
but i haven't had any acquisition problems yet.  


My real question here is:  What is the standard way to handle this
problem of being able to add dtml/zpt methods to classes such that all
instances of those classes get the new methods?


Thanks in advance for any insight.


Sincerely,

Keith Alperin

 

 