24.05.18 11:38, Neil Girdhar пише:
I was previously constructing an object like this:

tb = TemporalBehavior(**kwargs, **parameters)

where various subclasses were doing things like

def __init__(self, some_kwarg, some_other_kwargs, some_parameter, some_other_parameter):

Then I realized that I want to pass the paramters as a dictionary so that I can store it..  I changed the code to this:

def __init__(self, some_kwarg, some_other_kwargs, parameters):

but I still need "some_parameter", so I did

some_parmeter = parameters['some_parameter']
some_other_parmeter = parameters['some_other_parameter']

Great, but now I have to check that exactly the list of parameters that I need is being sent in, so I need to do something like

if set(parameters) != ('some_parameter', 'some_other_parameter'):
     raise ValueError

It might be nice to do instead

{'some_parameter': p, 'some_other_parameter': q} = parameters

p = parameters.pop('some_parameter')
q = parameters.pop('some_other_parameter')
if parameters:
    raise ValueError

or

p, q = (lambda some_parameter, some_other_parameter: some_parameter, some_other_parameter)(**parameters)

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to