question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python super() not implemented

See original GitHub issue

The following code generates SyntaxError: super() is only valid in derived class constructors

        class foo:
            def bar(self):
                print('bar')

        class foo2(foo):
            def bar(self):
                print('bar2')
                super().bar()
                # foo.bar(self) # workaround


The error message seems to imply it works in derived constructors, but using it there generates the same error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
kovidgoyalcommented, Feb 3, 2017

I’ll just note that as far as the DRYness of super() is concerned, that is trivially achieved, by aliasing

BaseClass = SomeClass

class Subclass(BaseClass):
   ...

Now you only need to rename SomeClass in one place. Not to mention that with modern refactoring tools, renaming a class everywhere it is used is trivial to do, except in the very rare case of a codebase with lots of different classes all named with the exact same name.

Also IMO even using super() in the simple inheritance case is an antipattern, because it makes your code much less readable. Now someone reading through your sub-class has to remember what the super class is everytime they see super(), and there is no easy way to jump to the definition of the superclass without first going to the top of the sub class to get the name of the super class.

Basically, super() improves (marginally, over aliasing) maintainability for someone very familiar with the code, at the cost of readability for everyone else.

0reactions
boxedcommented, Feb 4, 2017

As ever: amazing turn around time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How is super() in Python 3 implemented? - Stack Overflow
This case is used for class methods; the return value is obj. - If it is an instance, it must be an instance...
Read more >
Supercharge Your Classes With Python super()
Note: Technically, super() doesn't return a method. It returns a proxy object. This is an object that delegates calls to the correct class...
Read more >
Python NotImplementedError | How to Avoid ... - eduCBA
Python NotImplementedError Exception occurs at runtime when client characterized base classes; conceptual techniques should raise this exception when they ...
Read more >
Built-in Exceptions — Python 3.11.1 documentation
A list of the notes of this exception, which were added with add_note() . ... NotImplementedError and NotImplemented are not interchangeable, ...
Read more >
Python's Super Considered Harmful - James Y. Knight
In addition to the above problems, there is a huge backwards-compatibility problem in that using super() does not mesh well at all with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found