cdgriffith / Box

Python dictionaries with advanced dot notation access

Home Page:https://github.com/cdgriffith/Box/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: default to an empty dict for missing keys

ma-sadeghi opened this issue · comments

It'd be nice if any missing key defaults to an empty dict (to reduce boilerplate in some cases).

I think what you want then is just default_box=True https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#default-box

@cdgriffith Thank you so much, that's exactly what I wanted! Also, thank you for creating this great package!

@cdgriffith One more thing: I just noticed that when default_box=True, as soon as a non-existent key is accessed, it gets created, even if nothing is assigned to it:

>>> x = Box(default_box=True)
>>> x
Box({})
>>> x.a
Box({})
>>> x
Box({'a': {}})

I was expecting that this only happens if I do something like: x.a = SOMETHING. Is this something that can be achieved? Thanks!