Description
Feature or enhancement
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
#96145
https://mail.python.org/archives/list/python-dev@python.org/message/TYIXRGOLWGV5TNJ3XMV443O3RWLEYB65/
Proposal:
It was discussed in the context of adding controversial AttrDict. The reason for adding AttrDict was to allow JSON object attributes to be accessed as Python object attributes rather than dict keys. The reason for not using SimpleNamespace for this was that you need to use a wrapper lambda x: SimpleNamespace(**x)
, which makes this solution less obvious.
Supporting a positional argument in SimpleNamespace()
, which can be a mapping or an iterable of key-value pairs (like in the dict
constructor) will make using SimpleNamespace with JSON more convenient.
SimpleNamespace({'a': 1, 'b': 2})
is the same as SimpleNamespace([('a', 1), ('b', 2)])
is the same as SimpleNamespace(a=1, b=2)
.