Seite 1 von 1

one-line tree

Verfasst: Montag 23. April 2012, 23:51
von pillmuncher

Re: one-line tree

Verfasst: Dienstag 24. April 2012, 08:22
von jens
Ja, nett. Die Idee von http://stackoverflow.com/questions/6780 ... n-instance finde ich noch interessanter:

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
Ausgabe:

Code: Alles auswählen

{1: {2: {3: 'foobar'}}}