News for hackers. Stuff that matters.
Re:Python Tips and Traps by birken on Monday January 19, 2015 @02:06PM (#8913428)

  >> nested_dd = collections.defaultdict(lambda: nested_dd)
  >> nested_dd['a']['b']['c']['d'] = 'hello'
Re:Python Tips and Traps by jonathanpoulter on Monday January 19, 2015 @02:21PM (#8913517)
For reference, this is called autovivification. http://en.wikipedia.org/wiki/Autovivification#Python
Re:Python Tips and Traps by birken on Monday January 19, 2015 @04:20PM (#8914154)
Oops, messed this up (too late to edit). Thanks choochootrain for the correction:

  >>> NestedDD = lambda: collections.defaultdict(NestedDD)
  >>> nested_dd = NestedDD()
  >>> nested_dd['a']['b']['c']['d'] = 'hello'
does the proper thing.
Re:Python Tips and Traps by bdr on Monday January 19, 2015 @03:50PM (#8913991)
I don't think that's what they wanted:

    >>> nested_dd['d']
    'hello'
Re:Python Tips and Traps by choochootrain on Monday January 19, 2015 @03:57PM (#8914022)
try nested_dd = lambda: defaultdict(nested_dd)
Re:Python Tips and Traps by jamiesonbecker on Monday January 19, 2015 @04:15PM (#8914117)
Just. Wow.
Re:Python Tips and Traps by rnhmjoj on Monday January 19, 2015 @07:31PM (#8914873)

  from collections import defaultdict

  defaultobj = lambda: type('defaultobj', (defaultdict,), {
      '__getattr__': lambda self, x:    self.__getitem__(x),
      '__setattr__': lambda self, x, v: self.__setitem__(x, v)
      })(defaultobj)

  names = defaultobj()
  names.mammalia.primates.homo['H. Sapiens'] = 'Human being'

  print(names)
The same but also allows dot notation.