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?
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".
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:
Which results in: 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.