Re:Wield Python's super() like a Jedi
by lachenmayer on Thursday May 26, 2011 @02:22PM
(#2588861)
Nice article, but one thing struck me when reading the code examples posted in the article: What's the use of using kwds in __init__?
I would personally just have written this somewhat similar to this:
But then again I'm by no means what anyone could call a Python 'Jedi'. Can anyone explain to me why using kwds in this way would be better? Would it not lead to having to look up the declaration of the class for the exact names of arguments to pop() every time?
[ Parent
| on HN ]
Re:Wield Python's super() like a Jedi
by Peristarkawan on Thursday May 26, 2011 @02:29PM
(#2588890)
The reason for using keywords is that there is no guarantee that the next class in the MRO after ColoredShape will be Shape. When the instance is a ColoredShape, it will be, but if ColoredShape is subclassed with a more complex inheritance structure, then something else could end up in between them in the MRO.
Thus, ColoredShape.__init__ can't simply pass on shapename to the super method, because that might not be the argument expected by the next __init__ method in the MRO. Instead, each method needs to gracefully accept arbitrary arguments and then pass on whatever it received (minus what it consumed).