Quantcast
Channel: Recursively access dict via attributes as well as index access? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Recursively access dict via attributes as well as index access?

$
0
0

I'd like to be able to do something like this:

from dotDict import dotdictifylife = {'bigBang':           {'stars':               {'planets': []}           }       }dotdictify(life)# This would be the regular way:life['bigBang']['stars']['planets'] = {'earth': {'singleCellLife': {}}}# But how can we make this work?life.bigBang.stars.planets.earth = {'singleCellLife': {}}#Also creating new child objects if none exist, using the following syntax:life.bigBang.stars.planets.earth.multiCellLife = {'reptiles':{},'mammals':{}}

My motivations are to improve the succinctness of the code, and if possible use similar syntax to Javascript for accessing JSON objects for efficient cross platform development. (I also use Py2JS and similar.)


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images