« Back to home

Python's super()

I’ve been writing a lot of Python lately, and there are some things I love about it, some things I’m uncomfortable with, but nothing I hate.  The super() builtin function makes life a lot easier when you’re using Python for object oriented code and have some inheritance going on.  This blog post describes the best way I’ve seen it used so far.

First of all, in Python3 you no longer need to write “super(CurrentClass, self).foo”, “super().foo” does the right thing generally.  This is great, because it’s easier to change class names, or copy a line of code, without breaking everything.

The blog post describes a simple way of passing arguments to parent classes, too.  The classes must work together, passing all arguments as a dictionary, and callers must name the arguments.  I think that’s good practice anyway, especially because when you require inheritance things are already getting complex, and the extra verbosity helps make your intentions explicit.