On Thu, May 26, 2011 at 10:31 AM, Raymond Hettinger <pyt...@rcn.com> wrote: > I just posted a tutorial and how-to guide for making effective use of > super().
I posted this already on the HackerNews thread but it seems to have largely gone unnoticed, so I'm reposting it here. 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 luckily 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 OrderedCounter 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. -- http://mail.python.org/mailman/listinfo/python-list