Am I using super () correctly?

I made a small piece of code because I'm still trying to figure out the features of using super() . Why does this piece work with this TypeError ?

  a = SecondClass() TypeError: __init__() takes exactly 2 arguments (1 given) 

Then the SecondClass.meth() function should print a line, but I am explicitly missing something conceptually.

 class FirstClass (object): def __init__ (self, value): self.value = value print self.value class SecondClass (FirstClass): def meth (self): super (FirstClass,self).__init__(value = "I am a strange string") a = SecondClass() a.meth() 
+6
source share
5 answers

This is not related to super . You do not explicitly define __init__ for SecondClass , but since it inherits from FirstClass , it inherits FirstClass __init__ . Thus, you cannot create an object without going to the value parameter.

Change OK. The first point, as others have noted, is that you should always use the current class in your super call, not the super class โ€” in this case, super(SecondClass, self) . This is because super means "get the parent class of class x," so obviously you mean "get the parent of SecondClass" - this is FirstClass.

The second point is that it makes no sense to call the __init__ method inside meth . __init__ already called when the object is created. Or your subclass defines its own version, which can choose whether to call its own method super; or, as in this case, this is not the case, in which case the version of the superclass is called automatically.

Let me repeat, because I suspect that this is the missing part in your understanding: the whole point of the subclass is that everything that you do not specifically redefine is inherited in any case. super is only for when you want to redefine something, but use logic from the superclass anyway.

So here is a stupid example:

 class FirstClass(object): def __init__ (self, value="I am the value from FirstClass"): print value def meth(self): print "I am meth from FirstClass" def meth2(self): print "I am meth2 from FirstClass" class SecondClass(FirstClass): def __init__ (self): print "I am in SecondClass" super(SecondClass, self).__init__(value="I am the value from SecondClass") def meth(self): print "I am meth from SecondClass" a=FirstClass() # prints "I am the value from FirstClass" b=SecondClass() # prints *both* "I am in SecondClass" *and* "I am the value from SecondClass a.meth() # prints "I am meth from FirstClass" b.meth() # prints "I am meth from SecondClass" a.meth2() # prints "I am meth2 from FirstClass" b.meth2() # *also* prints "I am meth2 from FirstClass", because you didn't redefine it. 
+8
source

The code for SecondClass should look like this:

 class SecondClass (FirstClass): def meth (self): super (SecondClass,self).__init__(value = "I am a strange string") 
+3
source

The first argument to super () should be the current class, not the parent class:

 class SecondClass(FirstClass): def meth(self): super(SecondClass, self).__init__(value="I am a strange string") 

Python will find the actual function that will be called on its own. In this case, this is the parent class, but this may not be the case when multiple inheritance is involved.

+2
source

It should be

 super(SecondClass, self) 

See python documentation on super ()

+1
source

Fix meth function

 class SecondClass (FirstClass): def meth (self): super (SecondClass,self).__init__(value = "I am a strange string") 
+1
source

Source: https://habr.com/ru/post/891742/


All Articles