[Python-ideas] Thoughts on "extended mapping unpacking"

Serhiy Storchaka storchaka at gmail.com
Thu May 24 07:58:54 EDT 2018


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)



More information about the Python-ideas mailing list