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?
Instance attributes are not meant to be shared among all instances.
Assigning a persistant object over instances to an instance attribute is
perverting your design.
Remember that experienced programmers will automaticcaly trigger a
warning in their mind when reading your mutable maildirs. They 'll lose
time to realize that it was intended. Since there is much more standard
ways to do this (class attribute for instance), you should not use that
form.
Cleverness is the step mother of obfuscation.
JM
--
http://mail.python.org/mailman/listinfo/python-list