-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested dicts create nested attributes on Dict(), but not on update() #105
Comments
Hey @gnudiff - thanks for reaching out. In previous versions of addict we would convert "everything" to an instance of a This is the intended, although I guess sometimes, surprising, functionality! In order to get the dict nested, try doing Hope this helps! |
Also see #95 |
In that case, I would recommend putting this info in either project description or update method docstring, as it could be considered unexpected behaviour, and appears confusing (and as I mentioned, it is not the case with eg. AttrDict alternative, which does create recursive AttrDict objects on update). In case you could be curious, my personal case was that I wanted to use addict for dotted access to JSON-stored configuration files, with possibility of parsing config from several files sequentially. For that, I tried just to extend Dict with load() and save() methods, of which load() would call Dict.update() |
Awesome use case - have done that myself. As for your recommendations - they are noted. It is mentioned, but perhaps not explicitly enough. Thanks for using addict! |
+1 Just another vote for this. Overall, I really like your implementation, but the constructor and Perhaps a kwarg on update that allows the user to select whether Dict is deep or not? |
Thanks @doconix. I understand that this creates confusion. I'd be open to looking into changing it in a future major release, but I'm torn on exactly the best API is. There's four(?) ways to set attributes and items on a addict today
and I'd love for them to be consistent, while at the same time supporting both mutating and not mutating references to the objects being set. The kwarg suggestion is a good one - thanks. If you'd like, I'd happy to consider any pull requests for change of functionality, better documentation or anything else you see missing. However, I can not promise a merge of backwards-breaking functionality at this point. Thanks again! |
Hi @mewwts, Continuing our discussion in #129, after #130, this #105 seems to be a good candidate to work on next. What do you think? As @doconix mentions, the fact that Just for a quick comparison, in Dotsi and EasyDict, In Addict, couldn't Regards, Edit: Also, could you please clarify if the following >>> x = dict({"foo": {"bar": "baz"}})
>>> x.update({"foo": {"hello": "world"}})
>>> x
{'foo': {'hello': 'world'}}
>>>
>>> from addict import Dict
>>> y = Dict({"foo": {"bar": "baz"}})
>>> y.update({"foo": {"hello": "world"}})
>>> y
{'foo': {'bar': 'baz', 'hello': 'world'}}
>>> Thanks! |
pip installed addict 2.2.0 on Python 2.7.12/Ubuntu 16.04.5 LTS
If you create Dict() with initial data, all the nested dicts are converted to attributes.
If you update an existing Dict(), the nested dicts remain dicts and nested attributes are not created.
[This, by the way, is contrary to what happens with the two analogues AttrDict and EasyDict. Those two, on the other hand, do not support creation of nested attributes on the fly at assignment, like d=Dict() d.a.b.c=1 ]
The text was updated successfully, but these errors were encountered: