views:

163

answers:

3

When using Python's super() to do method chaining, you have to explicitly specify your own class, for example:


    class MyDecorator(Decorator):
        def decorate(self):
            super(MyDecorator, self).decorate()

I have to specify the name of my class MyDecorator as an argument to super(). This is not DRY. When I rename my class now I will have to rename it twice. Why is this implemented this way? And is there a way to weasel out of having to write the name of the class twice(or more)?

+4  A: 

The BDFL agrees. See Pep 367 - New Super for 2.6 and PEP 3135 - New Super for 3.0.

Peter Rowell
Link to the current version of the PEP: http://www.python.org/dev/peps/pep-3135/
sth
`super()` without arguments does *not* work on Python 2.6.1
J.F. Sebastian
+4  A: 

Your wishes come true:

Just use python 3.0. In it you just use super() and it does super(ThisClass, self).

Documentation here. Code sample from the documentation:

class C(B):
    def method(self, arg):
        super().method(arg)    
        # This does the same thing as: super(C, self).method(arg)
nosklo
A: 

you can also avoid writing a concrete class name in older versions of python by using

def __init__(self):
    super(self.__class__, self)
    ...
Autoplectic
No, that won't work because __class__ will give you the most specific subclass for the instance, not necessarily the one in the context of the class we are writing.
toby
I feel it's unfair to the author to down-vote this answer. This answer makes a naive but common misconception. It deserves stating here, along with toby's explanation of why this fails to work. There's worth in reporting methods which DON'T work, too.
gotgenes