[Zope] Bound methods, acquisition, __init__, Unpickleable object

Casey Duncan cduncan@kaivo.com
Mon, 09 Jul 2001 10:52:26 -0600


> 
> I think this code is doomed to raise an AttributeError exception,
> unless it would happen that Root has a func_name method. The idea
> was to call the func_name method of Spam class, not Root. I guess
> the following example is the only way to do it (I hope I'm
> mistaking here, really):
> 
> class Root(Persistent):
>         __doc__ = 'This is the products root object.'
>         def __init__(self):
>                 self.callbacks = []
>         def register_func(self, func_name, obj_path):
>                 self.callbacks.append((func_name, obj_path))
>         def call_functions(self):
>                 for (func, objn) in self.callbacks:
>                         to = self.aq_base
>                         for o in objn.split('.'):
>                                 to = getattr(to, o)
>                         getattr(to, func)()
> 
> That way just just find out the path to the object holding the
> method, and store a "pointer" to it:
> 
>         self.register_func(self.do_it, 'foo.bar.ni.spam')
>         # Where root_obj.foo.bar.ni.spam is the Spam-object.
> 
> Hrm. A simple method reference would be so much nicer, but you
> can't win everytime.
> 
> best regards,
> Bo Granlund

In that case I might suggest subclassing Traversable, and storing the
path to the function as a string (or tuple). Then use restrictedTraverse
to find the method and call it.

BTW: What exactly are you trying to do that necessitates this type of
method lookup?

-- 
| Casey Duncan
| Kaivo, Inc.
| cduncan@kaivo.com
`------------------>