Re: Improvement to SimpleNamespace
[Serhiy]
As a workaround you can use
object_hook=lambda x: SimpleNamespace(**x)
That doesn't suffice because some valid JSON keys are not valid identifiers. You still need a way to get past those when they arise: catalog.books.fiction['Paradise Lost'].isbn Also, it still leaves you with using setattr(ns, attrname, attrvalue) or tricks with vars() when doing updates. The AttrDict recipe is popular for a reason. Raymond
15.04.20 10:06, Raymond Hettinger пише:
[Serhiy]
As a workaround you can use
object_hook=lambda x: SimpleNamespace(**x)
That doesn't suffice because some valid JSON keys are not valid identifiers. You still need a way to get past those when they arise: catalog.books.fiction['Paradise Lost'].isbn Also, it still leaves you with using setattr(ns, attrname, attrvalue) or tricks with vars() when doing updates. The AttrDict recipe is popular for a reason.
Then it obviously should be different class than SimpleNamespace. There are too much differences between them, and they are used in different circumstances.
On Wed, Apr 15, 2020 at 2:12 AM Serhiy Storchaka <storchaka@gmail.com> wrote:
Then it obviously should be different class than SimpleNamespace. There are too much differences between them, and they are used in different circumstances.
+1 Keep in mind that I added SimpleNamespace when implementing PEP 421, to use for the new "sys.implementation". The whole point was to keep it simple, as the docs [1] suggest. -eric [1] https://docs.python.org/3/library/types.html#types.SimpleNamespace
participants (3)
-
Eric Snow
-
Raymond Hettinger
-
Serhiy Storchaka