nicholasmireles / DotDict

A simple Python library to make chained attributes possible.
GNU General Public License v3.0
231 stars 3 forks source link

sending dict on class instantiation? #5

Open gilbh opened 1 year ago

gilbh commented 1 year ago

I tried doing this:

In [5]: aa = ddict({'asdf': 1, 'xzcv': 2})
In [6]: aa
Out[6]: DotDict()

Would it be possible to pass the dict already in the class creation?

pkoryzna commented 3 months ago

yes, it should be possible with from_dict method, give it a try:

>>> from dotdict import DotDict
>>> nested = {"A": {"B": {"C": {"D": 1}}}}
>>> abcd = DotDict.from_dict(nested)
>>> abcd.A
DotDict(B=DotDict(C=DotDict(D=1)))
>>> abcd.A.B
DotDict(C=DotDict(D=1))
>>> abcd.A.B.C
DotDict(D=1)
>>> abcd.A.B.C.D
1