[Python-ideas] Dictionary destructing and unpacking.

Stephan Houben stephanh42 at gmail.com
Thu Jun 8 03:09:43 EDT 2017


Hi Lucas,

I would consider converting the dict into a namedtuple then.
Essentially the namedtuple acts as a specification for expected fielsds:

abc = namedtuple("ABC", "a b c")
d = {"a":1, "b": 2, "c":3} # presumably you got this from reading some JSON
abc(**d)
# returns: ABC(a=1, b=2, c=3)


Stephan

2017-06-08 8:53 GMT+02:00 Lucas Wiman <lucas.wiman at gmail.com>:
>> Maps with a known, fixed set of keys are relatively uncommon
>> in Python, though.
>
>
> This is false in interacting with HTTP services, where frequently you're
> working with deserialized JSON dictionaries you expect to be in a precise
> format (and fail if not).
>
> On Wed, Jun 7, 2017 at 11:32 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>>
>> C Anthony Risinger wrote:
>>>
>>> Incredibly useful and intuitive, and for me again, way more generally
>>> applicable than iterable unpacking. Maps are ubiquitous.
>>
>>
>> Maps with a known, fixed set of keys are relatively uncommon
>> in Python, though. Such an object is more likely to be an
>> object with named attributes.
>>
>> --
>> Greg
>>
>>
>> _______________________________________________
>> 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/
>
>
>
> _______________________________________________
> 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/
>


More information about the Python-ideas mailing list