is this foolish?

Cameron Simpson cs at zip.com.au
Thu Apr 12 05:35:27 EDT 2012


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?
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Real Daleks don't climb stairs.... Real Daleks level the building.
        - Adrian Tritschler, ajft at ajft_sun.cs.adfa.oz.au



More information about the Python-list mailing list