https://gist.github.com/2012250
one-line tree
- pillmuncher
- User
- Beiträge: 1484
- Registriert: Samstag 21. März 2009, 22:59
- Wohnort: Pfaffenwinkel
In specifications, Murphy's Law supersedes Ohm's.
- jens
- Python-Forum Veteran
- Beiträge: 8502
- Registriert: Dienstag 10. August 2004, 09:40
- Wohnort: duisburg
- Kontaktdaten:
Ja, nett. Die Idee von http://stackoverflow.com/questions/6780 ... n-instance finde ich noch interessanter:
Ausgabe:
Code: Alles auswählen
class RecursiveDict(dict):
"""Implementation of perl's autovivification feature."""
def __missing__(self, key):
value = self[key] = type(self)()
return value
d = RecursiveDict()
d[1][2][3] = "foobar"
print d
Code: Alles auswählen
{1: {2: {3: 'foobar'}}}