classification
Title: Add mapping methods to types.SimpleNamespace
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, rhettinger, shihai1991, v+python, veky, vstinner
Priority: normal Keywords:

Created on 2020-04-14 21:10 by rhettinger, last changed 2020-12-22 02:54 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
Dotable.py v+python, 2020-04-15 04:28
Messages (10)
msg366447 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-14 21:10
types.SimpleNamespace() would be much more usable if it were more substitutable for dictionaries.   In particular, it would be wonderful to be able to use object_hook=SimpleNamespace in json.load():

Current:

     catalog = json.load(f)
     print(catalog['clothing']['mens']['shoes']['extra_wide']['quantity'])

Proposed:

     catalog = json.load(f, object_hook=SimpleNamespace)
     print(catalog.clothing.mens.shoes.extra_wide.quantity])
msg366448 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-14 21:23
Only the magic methods need to be added:  __getitem__, __setitem__, and __delitem__, __contains__, __len__, and __iter__.

The non-dunder names risk incursion into user-space names.

>>> SimpleNamespace(username1='value1', username2='value2')
namespace(username1='value1', username2='value2')
>>> dir(_)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'username1', 'username2']
msg366449 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-04-14 21:31
I would prefer that SimpleNamespace remains as simple as it is:

https://docs.python.org/dev/library/types.html#types.SimpleNamespace
"A simple object subclass that provides attribute access to its namespace"

If you want to add the mapping protocol, I suggest you to create your own subclass and add the methods that you want.
msg366451 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-14 21:40
> I would prefer that SimpleNamespace remains as simple as it is

This doesn't affect the simplicity of the current API at all.  If you don't need the feature, you won't even notice the extension.

> If you want to add the mapping protocol, I suggest you to create
> your own subclass and add the methods that you want

I do that for myself.  However, users would greatly benefit from having this an option.

We don't ask users to write their own defaultdicts from scratch even though that is simple dict subclass.  Providing this tool would instantly benefit a broad class of json users.
msg366453 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-04-14 21:52
> Providing this tool would instantly benefit a broad class of json users.

I'm not saying that there is no need for such tool. I am just asking to leave SimpleNamespace unchanged.
msg366463 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-14 23:06
> I'm not saying that there is no need for such tool. 
> I am just asking to leave SimpleNamespace unchanged.

I really don't see the downside. It doesn't impair SimpleNamespace in any way.  Why would we add another type with substantially the same capabilities as SimpleNamespace?  That would be a complete waste.
msg366485 - (view) Author: Glenn Linderman (v+python) * Date: 2020-04-15 04:18
Yes, I laud this effort. I don't care if it is called SimpleNamespace (which I didn't use because it was insufficient), or anything else, but it would be extremely handy to have this battery.

I eventually found one called Dotable (or did I rename it to that?) which after a fair bit of study I finally understood, and then was able to add a few tweaks to make it work the way that was most convenient for me.

While I agree with Guido that these are not standard Python semantics, they are, as pointed out by Raymond, far more convenient to use for nested structures.

And as far as using CoffeeScript, I don't know of a CoffeeScript to Python conversion: the rest of the semantics of Python are more preferable than Javascript. I just wish Brendan Eich had consulted with Guido before inventing Javascript.
msg366486 - (view) Author: Glenn Linderman (v+python) * Date: 2020-04-15 04:28
Here's what I have.

Maybe it would be better if parse and dump were under or dunder names, although I think parse was in the original implementation I found.

Is this the derived from the same original as PyPI dotable? Not sure.
msg366499 - (view) Author: Vedran Čačić (veky) * Date: 2020-04-15 08:50
I think there is a teaching moment here. I think it's important that no object in Python standard library conflates "item namespace" with "attr namespace". (Maybe that isn't true because of some specialized objects, but surely general ones such as SimpleNamespace shouldn't commit that mistake.)

On the other hand, we do have json in standard library, and JSON is primarily based on JavaScript Object Notation (yes, I know it's been extended to fit more languages, but still, its primal DNA shows). And the conflating of attributes and items is so natural in JS (a['b']===a.b) that it took them more than a decate to realize that separating Map from Object might be a good idea. :-)

So, maybe we should also have something like JSNamespace in stdlib (presumably in json module). But SimpleNamespace shouldn't be it. As Guido once said, a bucket has a handle, and it contains sand, but it doesn't contain a handle.
msg366822 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-04-20 13:02
Raymond started a thread on python-dev:
https://mail.python.org/archives/list/python-dev@python.org/message/JOMND56PJGRN7FQQLLCWONE5Z7R2EKXW/

It seems like most core developers are against modifying types.SimpleNamespace, the trend is more about adding a different class.

About a different class, so far, I see no consensus on an API. I suggest to close this issue (which is about types.SimpleNamespace) and continue the discussion on python-dev.
History
Date User Action Args
2020-12-22 02:54:50rhettingersetstatus: open -> closed
stage: resolved
2020-12-22 02:42:30rhettingersetresolution: rejected
2020-04-20 13:02:37vstinnersetmessages: + msg366822
2020-04-19 09:02:47shihai1991setnosy: + shihai1991
2020-04-15 08:50:24vekysetnosy: + veky
messages: + msg366499
2020-04-15 04:28:09v+pythonsetfiles: + Dotable.py

messages: + msg366486
2020-04-15 04:18:16v+pythonsetnosy: + v+python
messages: + msg366485
2020-04-14 23:06:09rhettingersetnosy: + eric.snow
messages: + msg366463
2020-04-14 21:52:36vstinnersetmessages: + msg366453
2020-04-14 21:40:54rhettingersetmessages: + msg366451
2020-04-14 21:31:02vstinnersetnosy: + vstinner
messages: + msg366449
2020-04-14 21:23:18rhettingersetmessages: + msg366448
2020-04-14 21:10:49rhettingercreate