[ZODB-Dev] Pickling methods and workarounds

Christian Robottom Reis kiko at async.com.br
Fri Feb 27 18:30:18 EST 2004


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?

Take care,
--
Christian Robottom Reis | http://async.com.br/~kiko/ | [+55 16] 261 2331



More information about the ZODB-Dev mailing list