advanced python autovivification
Posted
by Zhang18
on Stack Overflow
See other posts from Stack Overflow
or by Zhang18
Published on 2010-03-12T21:24:17Z
Indexed on
2010/03/12
21:27 UTC
Read the original article
Hit count: 259
This question is about implementing the full PERL autovivification in python. I know similary questions were asked before and so far the best answre is http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python/652284#652284. However, I'm looking to do this:
a['x']['y'].append('z')
without declaring a['x']['y'] = []
first, or rather, not declaring a['x'] = {}
either. I know dict
and list
classes sorta don't mix so this is hard, but I'm interested in seeing if someone has an ingenius solution probably involving creating an inherited class from dict
but defined a new append
method on it? I also know this might throw off some python purists who will ask me to stick with Perl. But even just for a challenge, I'd like to see something. thx!
© Stack Overflow or respective owner