title: How to detect what element of a nested list has changed? (python)
body: <p>I have a large 2D (list of lists) list, each element containing a list of ints, strings and dicts. I would like to be able to determine the 'path' (eg. [2][3][2]["items"][2] at the worst!) of any element that is modified, and have this fire upon modification. This list is to big to scan through and see what has changed!  Ideally, I also want a copy of the new element although this can be found out later.</p>
  
  <p>My first attempt was to create a class, and override its <code>__setattr__</code> method:</p>
  
  <pre><code>class Notify():
      def __setattr__(self, name, value):
          self.__dict__[name] = value       #Actually assign the value
          print name, value                 #This will tell us when it fires
  </code></pre>
  
  <p>However, the <code>__setattr__</code> method only fires when setting a variable that is not accessed by an index (or key), because this seems to outsource the call to the contained list()/dict() class not our class.</p>
  
  <pre><code>&gt;&gt;&gt; test = Notify()
  &gt;&gt;&gt; test.var = 1          #This is detected
  var 1
  
  &gt;&gt;&gt; test.var = [1,2,3]    #Now let's set it to a list
  var [1, 2, 3]             
  
  &gt;&gt;&gt; test.var[0] = 12      #But when we assign a value via an index, it doesn't fire 
  &gt;&gt;&gt; test.var
  [12, 2, 3]                #However it still assigns the value, so it must be talking to the list itself!
  </code></pre>
  
  <p>So, to summarise, I want (any method really) that tells me at what (list of indexes/keys) a change has occurred, and this needs to happen as it occurs because it is too expensive to scan through the whole list. I also can't rely on the code that modifies the list to provide the details. If this is not possible to the nth nested list, I could use something that only gave the first two indexes as the data there would not be too large to scan. Thank you in advance for your help!</p>
  
  <p>EDIT: Still no joy, though this question <a href="https://stackoverflow.com/questions/8858525/track-changes-to-lists-and-dictionaries-in-python">Track changes to lists and dictionaries in python?</a> seems to be close to what I need. Unfortunately, I'm not very good with classes and need the help of someone who is!</p>
  
  <p>EDIT: Had a look at this <a href="https://stackoverflow.com/questions/21196912/python-right-way-to-extend-list?rq=1">Python: Right way to extend list</a> got me thinking that inheriting <code>list</code> might be a bad idea. I have come up with the following code using a proxy class instead. However the original problem remains, that modifications to nested lists don't log. Would class composition, not inheritance be a good idea?</p>
  
  <pre><code>from UserList import UserList
  
  class NotifyList(UserList):
  
      def __init__(self, initlist=None):
          self.data = []
          if initlist is not None:
              if type(initlist) is list:
                  self.data[:] = initlist
              elif isinstance(initlist, NotifyList):
                  self.data[:] = initlist.data[:]
              else:
                  self.data = list(initlist)
  
      def __setitem__(self, key, item):
          if type(item) is list:
              self.data[key] = NotifyList(item)
          else:
              self.data[key] = item
          print key, item
  
      def append(self, item):
          if type(item) is list:
              self.data.append(NotifyList(item))
          else:
              self.data.append(item)
          print self.index(item), item
  </code></pre>