[ZODB-Dev] Re: Pickling methods and workarounds

Casey Duncan casey at zope.com
Fri Feb 27 22:24:32 EST 2004


On Fri, 27 Feb 2004 20:30:18 -0300
Christian Robottom Reis <kiko at async.com.br> wrote:

> On Fri, Feb 27, 2004 at 07:35:50PM -0300, Christian Robottom Reis
> wrote:
> > So today I ended up having to modify the behaviour in one of my
> > classes, and my initial plan was to customize this behaviour using a
> > method stored as an instance attribute. So I had something like:
> 
> [...]
> 
> > situations like these. The ones I see at the moment are:
> > 
> >     - Storing the method name as an instance attribute, and doing:
> > 
> >         def __init__(self):
> >             self.plan_method = "_real_bar_A"
> > 
> >         def enable_plan_b(self):
> >             self.plan_method = "_real_bar_B"
> > 
> >         # ...
> > 
> >         def foo(self):
> >             getattr(self, self.plan_method)(0)
> > 
> >       which is quite ugly.
> > 
> >     - Using an if clause in foo(), which adds some minor overhead,
> >     but
> >       uglifies the code in every callsite that wants to call bar().
> 
>       - Providing __setstate__ and __getstate__ methods that set the
>         method and delete it based on an instance attribute used as a
>         flag.
> 
> Any other -- potentially nicer -- ways to do this?

Use callable objects instead? Usually when I find myself doing something
like this, it is a hint that maybe I should try another approach to the
problem altogether. Often dynamic composition can alleviate problems
like this.

-Casey




More information about the ZODB-Dev mailing list