is this foolish?

Cameron Simpson cs at zip.com.au
Thu Apr 12 06:04:55 EDT 2012


On 12Apr2012 19:43, Chris Angelico <rosuav at gmail.com> wrote:
| On Thu, Apr 12, 2012 at 7:35 PM, Cameron Simpson <cs at zip.com.au> 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
| >
| > Would experienced users please mock me?
| 
| Well, no mocking; the same technique on a non-method function is a
| pretty normal way of maintaining state.

I hadn't realised it was a common approach.

| But wouldn't it be simpler to use a class variable?

Yes. Yes it would.

| class RuleState(object):
|     maildirs = {}
|     def __init__(self, M, maildb_path):
| Then just use self.maildirs everywhere, never assign to it.

Thanks. It makes the shared state far more obvious, too.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Life IS pain, highness...  anyone who tries to tell you different is
trying to sell you something.   - Wesley, The_Princess_Bride



More information about the Python-list mailing list