Questions tagged [super]

super is a keyword or function used to access/invoke members and constructors of a superclass. Since different languages have such a feature, please use in combination with a language tag.

In Object Oriented programming, super is commonly used keyword to allow access to the members of the superclass from a derived class. It can also be used to select which superclass' constructor to execute when a subclass is created.

Although the concept is prevalent in many object oriented languages, every language has different usages for the keyword.

A few of the basic questions on super in different languages:

A superclass is also called base-class or parent-class and some languages use such keywords instead of super.

1757 questions
43
votes
1 answer

In python, super() is always called first in a method. Are there situations where it should be called later?

Are there situations where you want to do some processing before you call super()? This is a contrived example. Are there better examples? Is this considered pythonic? class Base(object): def __init__(self, name): print "Base %s…
TomOnTime
  • 4,175
  • 4
  • 36
  • 39
41
votes
5 answers

Python super() behavior not dependable

For some reason, the super() method is not always behaving as expected, opting to return: TypeError('super(type, obj): obj must be an instance or subtype of type)' I understand what the error means. I do not understand why it is coming up as an…
Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30
41
votes
2 answers

What's the difference between super() and Parent class name?

Is there a difference between using super() and using the parent class name directly? For example: class Parent: def __init__(self): print("In parent") self.__a=10 class Child(Parent): def __init__(self): …
41
votes
4 answers

Inheriting from a namedtuple base class

This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b,…
alvas
  • 115,346
  • 109
  • 446
  • 738
40
votes
5 answers

Meaning of Super Keyword

What's the meaning and usage of the super keyword in Java?
Tushar
  • 5,907
  • 15
  • 49
  • 81
39
votes
2 answers

what does super() do without any arguments?

I'm learning react from the docs, but not sure what the super() does in this example. Usually, doesn't it take the arguments that are passed to making a new instance and then calls React.Component's constructor method to incorporate these arguments…
mangocaptain
  • 1,435
  • 1
  • 19
  • 31
37
votes
3 answers

Why do we have to use the __dunder__ methods instead of operators when calling via super?

Why do we have to use __getitem__ rather than the usual operator access? class MyDict(dict): def __getitem__(self, key): return super()[key] We get TypeError: 'super' object is not subscriptable. Instead we must use…
wim
  • 338,267
  • 99
  • 616
  • 750
36
votes
3 answers

Python: 'super' object has no attribute 'attribute_name'

I am trying to access a variable from the base class. Here's the parent class: class Parent(object): def __init__(self, value): self.some_var = value And here's the child class: class Child(Parent): def __init__(self, value): …
invarbrass
  • 2,023
  • 4
  • 20
  • 23
36
votes
2 answers

Pylint warning for "useless super delegation"

Pylint raises the warning: Useless super delegation in method '__init__' (useless-super-delegation) for the SpecificError class below. class MyProjectExceptions(Exception): """The base class for all my project's exceptions.""" def…
mugwump
  • 436
  • 1
  • 4
  • 10
35
votes
2 answers

"MetaClass", "__new__", "cls" and "super" - what is the mechanism exactly?

I have read posts like these: What is a metaclass in Python? What are your (concrete) use-cases for metaclasses in Python? Python's Super is nifty, but you can't use it But somehow I got confused. Many confusions like: When and why would I have…
JV.
  • 2,658
  • 4
  • 24
  • 36
34
votes
8 answers

Require override of method to call super

I want that when a child class overrides a method in a parent class, the super.method() is called in that child method. Is there any way to check this at compile time? If not, how would I go about throwing a runtime exception when this happens?
Andrew Gaspar
  • 528
  • 1
  • 4
  • 8
34
votes
2 answers

How does multiple inheritance work with the super() and different __init__() arguments?

I'm just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) What's wrong with…
kramer65
  • 50,427
  • 120
  • 308
  • 488
33
votes
14 answers

Force base method call

Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a compile-time error if it isn't called? That would be very…
irwinb
  • 993
  • 2
  • 10
  • 20
33
votes
12 answers

Any way to _not_ call superclass constructor in Java?

If I have a class: class A { public A() { } } and another class B extends A { public B() { } } is there any way to get B.B() not to call A.A()?
Миша Кошелев
  • 1,483
  • 1
  • 24
  • 41
33
votes
5 answers

How to find source_location of the code executed by super?

class C1 def pr puts 'C1' end end class C2 < C1 def pr puts 'C2' super puts self.method(:pr).source_location end end c = C2.new c.pr In the program above is it possible to obtain location of the code executed by super…