On 04/12/2012 10:35 AM, Cameron Simpson wrote:
I've found myself using a Python gotcha as a feature.

I've got a budding mail filter program which keeps rule state in a
little class instance. Slightly paraphrased:

     class RuleState(object):
         def __init__(self, M, maildb_path, maildirs={}):
             [...]
             self.maildirs = maildirs

The maildirs property is a cache of Maildir objects mapped by their
pathname to avoid opening Maildirs every time they're mentioned. I
create a new RuleState every time I file a message, and of course I want
to share the cache between instances.

Normally we look on the Python default parameter value as a gotcha which
causes the unwary to reuse a single object across the board, causing
unwanted persistence of state.

But here I actually think this is a sane way to make an anonymous single
shared state object for the maildirs cache, absent the caller's intent
to use their own.

I can think of a few potential downsides, but on the whole this is going
to do exactly what I want in this case.

Would experienced users please mock me?

You could probably just make this caching more explicit using a decorator.
The good thing about that is that you could even change the behaviour of your
caching without touching the code that uses it for example.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to