Is there are good DRY fix for this painful design pattern?

Chris Angelico rosuav at gmail.com
Mon Feb 26 10:06:07 EST 2018


On Tue, Feb 27, 2018 at 1:41 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> I have a class with a large number of parameters (about ten) assigned in
> `__init__`. The class then has a number of methods which accept
> *optional* arguments with the same names as the constructor/initialiser
> parameters. If those arguments are None, the defaults are taken from the
> instance attributes.

I have a very similar problem in a project of mine, and it's currently
an unsolved problem. My current best theory is to basically just
accept **kwargs for everything (so there's no None defaults), and then
use chain_map for all lookups. Seems inefficient, but at least it'd be
functional. Haven't implemented it though.

If you get any better ideas, I'll be reading them too.

ChrisA



More information about the Python-list mailing list