17

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 Answers 4

12

In Python 3.0, you can use super() which is equivalent to 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)
Sign up to request clarification or add additional context in comments.

Comments

7

The BDFL agrees. In Python 3.0, support for super() with no arguments was added:

PEP 3135: New super(). You can now invoke super() without arguments and (assuming this is in a regular instance method defined inside a class statement) the right class and instance will automatically be chosen. With arguments, the behavior of super() is unchanged.

Also see Pep 367 - New Super for Python 2.6.

Comments

4

This answer is wrong, try:

def _super(cls):
    setattr(cls, '_super', lambda self: super(cls, self))
    return cls

class A(object):
    def f(self):
        print 'A.f'

@_super
class B(A):
    def f(self):
        self._super().f()

@_super
class C(B):
    def f(self):
        self._super().f()

C().f() # maximum recursion error

In Python 2 there is a way using decorator:

def _super(cls):
    setattr(cls, '_super', lambda self: super(cls, self))
    return cls

class A(object):
    def f(self):
        print 'A.f'

@_super
class B(A):
    def f(self):
        self._super().f()

B().f() # >>> A.f

Comments

-1

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

def __init__(self):
    super(self.__class__, self)
    ...

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.