News for hackers. Stuff that matters.
Re:Wield Python's super() like a Jedi by Peristarkawan on Thursday May 26, 2011 @02:01PM (#2588756)
It seems to me that the example of combining built-in dictionary classes is naively optimistic. For starters, OrderedDict, as it happens, does not use super! It calls the dict super-class methods directly. Since dict happens to be the next class in the MRO, this doesn't really matter for the purpose of this example, but I can envision a scenario where some plucky programmer inherits from both OrderedCounter and some other dict subclass, and the result doesn't work because OrderedDict does the wrong thing.

And OrderedDict isn't the only one. Maybe for some reason I would like to have an OrderedCounter where all the counts default to 42. So I do this:

  class DefaultOrderedCounter(defaultdict, OrderedCounter):
      pass
  doc = DefaultOrderedCounter(lambda: 42)
  doc.update('abracadabra')
Which results in:

  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "c:\python32\lib\collections.py", line 507, in update
      _count_elements(self, iterable)
    File "c:\python32\lib\collections.py", line 63, in __setitem__
      self.__map[key] = link = Link()
  AttributeError: 'DefaultOrderedCounter' object has no attribute '_OrderedDict__map'
Whoops! Apparently defaultdict doesn't use super either. Of course a better way to do this would be to subclass DefaultOrderedCounter and just override the __missing__ method by hand, but that's not the point.

The article goes into "How to Incorporate a Non-cooperative Class", which basically says "wrap it up in a proxy class". But that's not really going to work here, since the result would be two separate dicts, with the defaultdictwrapper methods operating on one dict, and the other methods operating on the other.

Re:Wield Python's super() like a Jedi by Peristarkawan on Thursday May 26, 2011 @02:09PM (#2588785)
Okay, I give. What's the markup for posting code snippets on this site?
Re:Wield Python's super() like a Jedi by lmkg on Thursday May 26, 2011 @02:15PM (#2588815)
Put two spaces at the beginning of a line for that line to be displayed monospaced.

  Example
Re:Wield Python's super() like a Jedi by simcop2387 on Thursday May 26, 2011 @09:34PM (#2590117)
why would you have to subclass DefaultOrderedCounter again in order to add in the missing method?

Though I agree there's something annoying going on there with the multiple inheritence.

Re:Wield Python's super() like a Jedi by Peristarkawan on Friday May 27, 2011 @01:57AM (#2590566)
I mistyped. I meant "subclass OrderedCounter to add in the __missing__ method".