Coder Social home page Coder Social logo

Comments (8)

mewwts avatar mewwts commented on September 28, 2024

Hey @gnudiff - thanks for reaching out.

In previous versions of addict we would convert "everything" to an instance of a Dict. This led to some surprising results where the items you put into a Dict was in fact not referenced by the Dict (since the Dict held a copy). This is why the constructor is the only part of addict which will recursively transform a dict to a Dict.

This is the intended, although I guess sometimes, surprising, functionality! In order to get the dict nested, try doing y.update(Dict(d)).

Hope this helps!

from addict.

mewwts avatar mewwts commented on September 28, 2024

Also see #95

from addict.

gnudiff avatar gnudiff commented on September 28, 2024

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()

from addict.

mewwts avatar mewwts commented on September 28, 2024

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!

from addict.

doconix avatar doconix commented on September 28, 2024

+1

Just another vote for this. Overall, I really like your implementation, but the constructor and update having different behavior was a gotcha for me. It seems like they should be consistent with one another.

Perhaps a kwarg on update that allows the user to select whether Dict is deep or not?

from addict.

mewwts avatar mewwts commented on September 28, 2024

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

a.b = 2
a['b'] = 2
a = Dict(somedict)
a.update(otherdict)

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!

from addict.

mewwts avatar mewwts commented on September 28, 2024

@gnudiff @doconix any interest in attempting to find a consisten API across the four methods I outlined above? As mentioned happy to receive a PR.

from addict.

sumukhbarve avatar sumukhbarve commented on September 28, 2024

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 .__init__() and .update() behave differently is a bit weird. And while documentation adds clarity, it doesn't really make such behavior fully non-weird.

Just for a quick comparison, in Dotsi and EasyDict, .update(), .__init__(), .__setattr__() and .__setitem__() all work the same way. And with regard to #95, Dotsi retains references too.

In Addict, couldn't .__init__() and .update() be implemented as aliases? Or, could one call the other? I realize that we'll need to be careful about backward compatibility, but could you please indicate the specific compatibility issues we'll need to be vigilant of? Clarity regarding which aspects should be preserved would be helpful.

Regards,
S

Edit: Also, could you please clarify if the following .update() behavior is desired:

>>> 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!

from addict.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.