I'm learning Python and I'm finding it difficult to understand what the super()
method does in a class. Could somebody kindly explain it and its usecases in simple words?
Asked
Active
Viewed 44 times
-3
-
see: https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods – jmunsch Jan 31 '20 at 19:10
-
super can be used to call any parent class method, including init(). – quamrana Jan 31 '20 at 19:12
-
5I'm not a fan of downvotes with no explanation but I will assume the reason is because this question can be easily answered by looking at the language's documentation or an easy search on a search engine. – Robert Brisita Jan 31 '20 at 19:13
-
2Please avoid placing footnotes indicating what the community should or should not do, the votes are not intended to offend or praise users but only qualify the post, those are the rules of SO. Besides pointing out that kind of thing in practice they get the opposite: more downvotes. – eyllanesc Jan 31 '20 at 19:13
-
Thank you everyone, I appreciate your help very much. I'm just getting started I'm sorry for placing footnotes. – eyelid Jan 31 '20 at 19:16
-
@eyelid The goal of super in simple words is to invoke the implementation of the parent class. – eyllanesc Jan 31 '20 at 19:17
-
@eyelid see this answer: https://stackoverflow.com/a/27134600/6622587 – eyllanesc Jan 31 '20 at 19:21
-
@eyllanesc thank you, i've finally understood it that it inherits __init__ of parent to my child class so parent class __init__ content can be called from my child class by `self` – eyelid Jan 31 '20 at 19:24
-
@eyllanesc please correct me if im wrong, does `super().__init__()` inside child class only brings content of parent class `__init__` to child class? because i can call functions of `ParentClass` from my `ChildClass` by just `self.funcname()` since my ChildClass object is `ParentClass` – eyelid Jan 31 '20 at 19:37
-
@eyelid I am going to propose a case, let's say that for some reason you want to add **more functionalities** to the "funcname" method: `def funcname(self):` `# more code` `super().funcname()` `# more code`, in this case if you change `super().funcname()` by `self.funcname()` you're going to have an infinite recursion – eyllanesc Jan 31 '20 at 19:42
-
@eyelid In your example there is no difference because by default if you do not override the children's method X then the parent's method X will be invoked. – eyllanesc Jan 31 '20 at 19:44
-
@eyelid But in general the `__init__` method is always overridden so it is common to invoke `super()` in that case – eyllanesc Jan 31 '20 at 19:45
-
@eyllanesc Please can you move this discussion to chat, I'd like to point out some things, I cannot do that SO says I don't have much reputation – eyelid Jan 31 '20 at 19:55
-
1Unfortunately I do not like to have extensive discussions so I will not answer anything. Bye. – eyllanesc Jan 31 '20 at 19:59
-
Welcome. SO is not designed to answer questions like this, and asking for tutorials or questions of that nature is against the guidelines of questions this site accepts. Please read [How To Ask](http://stackoverflow.com/help/how-to-ask), [What CAN I ask](https://stackoverflow.com/help/on-topic), [What NOT to Ask](https://stackoverflow.com/help/dont-ask) before your next post. Fortunately, there are many websites that *are* geared to handle questions like the one you posed, *and* there are many websites, easily found via the search engine of your choice, that explain such topics in detail. – SherylHohman Jan 31 '20 at 20:35
1 Answers
0
super
is used to make call to constructor of parent
class
At a fairly abstract level, super() provides the access to those methods of the super-class (parent class) which have been overridden in a sub-class (child class) that inherits from it.
Reference : https://www.geeksforgeeks.org/python-super-in-single-inheritance/

Umair Mohammad
- 4,489
- 2
- 20
- 34
-
Does super().__init__() within child class basically bring parent class variables to child class? – eyelid Jan 31 '20 at 22:19
-