Class with too many parameters: better design strategy?

后端 未结 13 927
醉酒成梦
醉酒成梦 2020-12-04 08:29

I am working with models of neurons. One class I am designing is a cell class which is a topological description of a neuron (several compartments connected together). It ha

13条回答
  •  北海茫月
    2020-12-04 08:51

    UPDATE: This approach may be suited in your specific case, but it definitely has its downsides, see is kwargs an antipattern?

    Try this approach:

    class Neuron(object):
    
        def __init__(self, **kwargs):
            prop_defaults = {
                "num_axon_segments": 0, 
                "apical_bifibrications": "fancy default",
                ...
            }
            
            for (prop, default) in prop_defaults.iteritems():
                setattr(self, prop, kwargs.get(prop, default))
    

    You can then create a Neuron like this:

    n = Neuron(apical_bifibrications="special value")
    

提交回复
热议问题