Why is it necessary to assign a class name when using the super () function in Python

Possible duplicate:
Why should I specify my own class when using the super () function, and is there any way around it?

I read this book - "Core Python Programming" , and I really found it very beautiful, I would say. But I was embarrassed at some point when I was studying the topic of Inheritance.

The book said somewhere that we can use super() to call the super class method , which will find the base class method for us, and we don’t need to explicitly skip self , as we do without super.

Here's an example code: -

 # Invoking super class method without super() .. Need to pass `self` as argument class Child(Parent): def foo(self): Parent.foo(self) # Invoking with super(). # No need to pass `self` as argument to foo() class Child(Parent1, Parent2): def foo(self): super(Child, self).foo() print 'Hi, I am Child-foo()' 

My question is why should we pass the classname to super() call .. Since the class name can be inferred from the class from which super is called.

So, since super () is called from class Child , the class name must be implicit. So why do we need this?

* EDIT: - Providing Child as a super () parameter does not make sense, because it does not give any information .. We could use super(self) .. And the method would be found in superclasses in the order in which they are indicated inside brackets.

+4
source share
1 answer

By providing the class name as the first argument, you are providing redundant information. Yes. This is a bit silly * and why super behavior changes in Python 3.

* With my answer, I actually contradict John Carter's answer. Of course, only one of us is right. I don't want to insult him and others, and I'm happy to see a meaningful example where super(C, self).method(arg) is not actually used in the C class super(C, self).method(arg) .

+4
source

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


All Articles