Posts Tagged ‘super’

h1

KISS (Keep It Simple, Super)

June 28, 2008

Remember this?

I start cussing every time I come across that post :)

Not to say it’s not a great post, going to the trouble of writing all that down in such a lucid, careful manner was positively a service to the rest of us. Thanks, Foom.

It’s just that, the whole thing with super is so broken. Which brings to mind a conversation over sushi, about Python:

Friend: “Oh, that scripting language. Yeah I remember that, man that’s got such a broken object model.”

Me: (Squirms)

There wasn’t really much to say, was there? Super, new-style classes vs. old-style classes, can’t raise new-style objects, yada yada yada? Aieh…

End rant. What really I wanted to do here is write down the condensed version of Fuhm’s post, as a series of simple rules I can go back to. Here goes -

  1. Don’t mix regular classes with exception classes
  2. All regular classes should be new-style, derived from object
  3. They should have the constructor signature __init__(self, **kwargs)
  4. Every such __init__ invokes super(ClassName, self).__init__(**kwargs)
  5. Never extend a new-style class that doesn’t follow these rules
  6. Don’t use multiple inheritance for exceptions
The point is that if you follow these rules then Python’s super behaves pretty much like Dylan’s next-method, and then Fuhm’s objections are moot, as long as you stick with the rules…
And there is not really any reason to bother with this unless you want to be prepared for multiple inheritance. Even if you are doing multiple inheritance you might prefer simple, stateless mix-ins – at least I do – in which case you don’t need an __init__ (there is no state) and life is somewhat easier (although, according to Fuhm you still need the placeholder __init__ with a direct super call to object.__init__).
Now, I wonder if Python3000 really fixes this?