Is there are good DRY fix for this painful design pattern?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Feb 26 23:58:08 EST 2018
On Tue, 27 Feb 2018 09:15:12 +1100, Ben Finney wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>
>> 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.
>
> The catalogue in Refactoring identifies what you need:
>
> Introduce Parameter Object
>
> You have a group of parameters that naturally go together.
>
> Replace them with an object.
>
> <URL:https://refactoring.com/catalog/introduceParameterObject.html>
Interesting, I'll certainly have a good read of that, thanks. But I fear
I mislead you: you seem to have understood that either all the parameters
are None, or none of them are, whereas what I meant was that they can
vary independently of each other.
So it isn't enough to check whether the Parameter object is None, I'd
need to check each field within the object.
On the other hand, at least I could put the logic of that inside the
Parameter object, instead of repeating it everywhere it is needed.
On the gripping hand, I *think* that means the caller is responsible for
creating and populating the Parameter object, instead of just passing
keyword arguments... let me think about that one.
Either way, thanks for the suggestion.
--
Steve
More information about the Python-list
mailing list