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

Neil Girdhar mistersheik at gmail.com
Thu May 24 11:46:29 EDT 2018


On Thu, May 24, 2018 at 8:00 AM Serhiy Storchaka <storchaka at gmail.com>
wrote:

> 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
>
> parameters is a Mapping subclass and I don't want to destroy it

> or
>
> p, q = (lambda some_parameter, some_other_parameter: some_parameter,
> some_other_parameter)(**parameters)
>
> that works

> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/rupoMkwAhi0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180524/275698f9/attachment.html>


More information about the Python-ideas mailing list