is this foolish?

Chris Angelico rosuav at gmail.com
Thu Apr 12 05:43:25 EDT 2012


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. But wouldn't it be simpler to
use a class variable?

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

Then just use self.maildirs everywhere, never assign to it.

ChrisA



More information about the Python-list mailing list