Monday, January 15, 2007

Python's super() Abused as a Hook

There has been some recent-ish discussion on the python-dev mailing list about "fixing" the super built-in, which is used to access attributes of an object with lookup rules on the superclass of a given class. This is used for different things and in different ways, but the most common usage seems to be as follows:

class Foo(Bar):
def action(self):
super(Foo, self).action()
self.actionCalledOnFooInstance = True

This causes a call to Foo().action() to call the action method of the next class in the Method Resolution Order. Now, Bar.action might exist, or maybe Bar inherits from Baz and Baz.action() will be called. The point is, you don't have to know. The typical pattern here is the that we are looking for the superclass of the same class we are within (Foo) and call the same method we're already in (action), which is a repetition some people want to fix.

I propose that super() is not broken at all, or even failing, but simply that we are misusing it where anothing idiom may be more appropriate. The more appropriate idiom might be hooks. Sometimes our function using super() should actually be calling a hook and other times it should be the hook. In the example above, we want something to happen after the regular action() method is called, so we redefine the entire action() method to call the original and then execute our one little line. We require all the overhead and issues with super() largely for varients of this common use case. Instead, consider if we wrote the following:

class Foo(Bar):
def hook_after_action(self):
self.actionCalledOnFooInstance = True

Do you see how much simpler that is? The requirement here is that either super(Foo, self).action would call hook_after_action if it exists (or there could be a default no-op version) or some mechanism outside of the action() method might handle it, perhaps wrapping it up on request or at definition. Maybe a standard hook format could even be a candidate for brining into the language.

Hooks are very valuable concepts that are not used enough. We can save ourselves a lot of trouble by using them. There is a lot more I could say about them, but this post was mostly about their relation to the many use cases of super.

PS - Some of this can be known to relate to the concept called "Aspect-Oriented Programm" which has very little weight in the Python world, because here its so easy to do that it doesn't deserve a name and is reduced to simply wrapping functions or having hooks.

3 comments:

fumanchu said...

Within reason, yes. Classes which have a few, standard extension points are better written as hooks. But there are classes (such as adapters) where potentially every method needs to be extended. Adding a pre hook and a post hook to every method would be silly, polluting the namespace unnecessarily. The choice of when to add a hook method shouldn't be left to a whim.

One option is to isolate the hooks into a separate Hookset object, so that the space of hook names is at least isolated from the class namespace.

Ludvig said...

"...the super built-in, which is used to access attributes of an object with lookup rules on the superclass of a given class."

What you're saying here isn't entirely correct and much of the Python community is wrong here.

To lay it quick, what super() actually does is that it returns a proxy object of the attribute that would've been returned if the current function hadn't been called. The difference here is when it comes to MRO - multiple inheritance.

I suggest reading http://fuhm.net/super-harmful/ - it explains what I mean better than I can do quickly here.

Anonymous said...

This is wrong in a couple of ways. First, you do need to know whether baz.action is being called or bar.action is being called. Things are going to be screwed up, and you're going to be debugging, and it's going to be important that you know exactly which routine was called when.

Secondly, it's going to be the case that you want both of baz.action and bar.action to be called. The hook approach won't work. As soon as you declare your hook, it overrides the hook in the superclass that you wanted to have called. So, your hook needs to call super(foo, self).hook().

I write here about programming, how to program better, things I think are neat and are related to programming. I might write other things at my personal website.

I am happily employed by the excellent Caktus Group, located in beautiful and friendly Carrboro, NC, where I work with Python, Django, and Javascript.

Blog Archive