[Zope-dev] traversing from zope product

Dieter Maurer dieter@handshake.de
Wed, 22 Aug 2001 23:41:09 +0200 (CEST)


Vincenzo Di Somma writes:
 > in our zope product (a folderish class) we want to call one of its 
 > contained methods (like a dtml-method, for example). We also want to 
 > decide which method (of all dtml-methods contained in the folderish 
 > item) at run-time... We tried using eval('self.' + method_name) and it 
 > works... The problem is it can't refer any other object in the same 
 > folder (more in general it can't traverse the zope tree)... what can we do?
When you want to traverse, use "restrictedTraverse"....

 > An example: in our "ff" folderish item there is a "mh" mailhost item and 
 > a "dm" dtml-method. We call "dm" from one of our "ff" methods (say 
 > "ff.mm") using eval("self.dm"). The result is that "dm" is correctly 
 > called but it won't find its "mh" mailhost (needed to send the mail).
Seems to be different problem: context not passed...
A standard error with DTML objects....

 > 1) Our code:
 > 
 > class ourclass (Folder)
 > 
 > def ourmethod(self, ...):
 > 	method_name=...  #will be "dm" at run-time
 > 	parameters=...	 #parameters for "dm"
 > 	eval("self." + method_name)(parameters=parameters)
Use:

	getattr(self,method_name)(self,self.REQUEST,parameters=parameters)

The replacement of "eval" by "getattr" is not essential.
The extra positional arguments are!


Dieter