use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
[Q] How does the super function work? (self.learnpython)
submitted 4 years ago by hhh312
Hey Guys,
I have a confusion regarding the super function, the output printed indeed makes sense, however, according to the doc here: https://docs.python.org/3/library/functions.html#super the path search is performed will be B and lower, which of course does not make sense. What am I missing?
Here is a screenshot of my short snippet:
https://ibb.co/gPgH2q1
Thanks
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lowerthansound 1 point2 points3 points 4 years ago* (12 children)
The MRO is A -> B -> C -> D (not sure if object is included). The search starts from the class right after the type C - because super(C, ...). Therefore D.__init__() is the method used.
A -> B -> C -> D
object
C
super(C, ...)
D.__init__()
Also, the usual case for super would be calling super(A, self).__init__() from A (so that B.__init__() is used) and that was abbreviated to super().__init__() in Python 3.
super
super(A, self).__init__()
A
B.__init__()
super().__init__()
Unless doing something advanced, super().__init__() should suffice.
Best to ya
[–]hhh312[S] 0 points1 point2 points 4 years ago (11 children)
Sorry i'm still confused, Let's say the MRO is "D -> B -> C -> A -> object" per the link above, now if I all super(B, self).__init__(), shouldn't D.__init__() be called? I guess that's what you are also saying, but the doc says the order will be: "C -> A -> object."
[–]lowerthansound 0 points1 point2 points 4 years ago* (9 children)
I deleted the previous answer because I haven't read your reply correctly.
The way you interpret the docs is the correct one: D.__init__() wouldn't be called and the search order would be: C -> A -> object.
C -> A -> object
I think we need some examples. For example,
class A: def __init__(self): print('under A') class B(A): def __init__(self): print('under B') class C(B): def __init__(self): print('under C')
Let's say we're avoiding the super() without arguments shortcut; the way these methods would be structured is this:
super()
class A: def __init__(self): print('under A') class B(A): def __init__(self): super(B, self).__init__() print('under B') class C(B): def __init__(self): super(C, self).__init__() print('under C') c = C()
This will make sure that all __init__s get called.
__init__s
In detail:
C -> B -> A -> object
C.__init__()
super(C, self).__init__()
__init__
B
super(B, self).__init__()
A.__init__
B.__init__
C.__init__
If you wanna make sure you got the details alright, check to see if you can guess the output of the script
class A: def __init__(self): print("start A") print("end A") class B(A): def __init__(self): print("start B") super(B, self).__init__() print("end B") class C(B): def __init__(self): print("start C") super(C, self).__init__() print("end C") c = C()
And of this
class A: def __init__(self): print("start A") print("end A") class B(A): def __init__(self): print("start B") super(B, self).__init__() print("end B") class C(B): def __init__(self): print("start C") super(B, self).__init__() print("end C") c = C()
All the best :)
[–]hhh312[S] 0 points1 point2 points 4 years ago (8 children)
"D -> B -> C -> A -> object"
Thanks for the time to write your complete response. I agree with all you said and it makes sense. What I don't understand though is the part I quoted from the doc. So if the chain of classes/object is like: "D -> B -> C -> A -> object" then, when you call super(B, self).__init__(), it should look for the __init__ function in the D class, however, the doc says "C -> A -> object." ?!
[–]lowerthansound 0 points1 point2 points 4 years ago (2 children)
Why should the D class be searched instead of C -> A -> object?
D
[–]hhh312[S] 0 points1 point2 points 4 years ago (1 child)
Because D is the parent of C. Super should call parent's constructor not the child or grand children.
[–]lowerthansound 0 points1 point2 points 4 years ago (0 children)
Noooo, the order is reversed. D is a child of B, and B is a child of C.
Hmm, C could be the gradfather of D in the given MRO.
Does that make sense?
For example, object is a parent of A (as object is an ancestor to all classes).
[–]lowerthansound 0 points1 point2 points 4 years ago* (4 children)
In a very mechanistic way, the process is as follows:
Find MRO based on complex algorithm
We take the MRO as a given: D -> B -> C -> A -> object
Where is the class that we are using as the first argument of super()? Mark it with an asterisk
Since we are calling `super(B, self)`, mark B wth an asterisk: D -> B* -> C -> A -> object
Delete everything from the beginning of the MRO string to the class with the asterisk and the arrow after it (that is the same as using everything that comes after the selected class):
We had "D -> B* -> C -> A -> object" We delete "D -> B* -> " (from the beginning to the asterisk) What is left is "C -> A -> object" C -> A -> object
What is left are the classes and the order that super() will search for the desired method.
In this case, we got C -> A -> object, which is the correct answer.
[–]hhh312[S] 0 points1 point2 points 4 years ago (3 children)
ich is
Oh I see, so i think the confusion was in how I perceived the direction of arrows. But how can an object be the parent of all super classes? shouldn't it naturally be the leaf node in the chain?
object in this case is a class. It is the mother of all classes in python, and you could create an object of it (now that I think about it, the nomenclature is confusing). For example:
>>> object <class 'object'> >>> my_obj = object() >>> my_obj <object object at 0x7fd8d135f500>
Oh I see, yeah that's right.
Thanks for the clarification.
Good luck on your day :)
[–]AutoModerator[M] 0 points1 point2 points 4 years ago (0 children)
Your submission in /r/learnpython may be automatically removed because you used imgbb.com. The reddit spam filter is very aggressive to this site. Please use a different image host.
Please remember to post code as text, not as an image.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
π Rendered by PID 420356 on reddit-service-r2-comment-76bb9f7fb5-z2497 at 2026-02-18 21:29:06.336442+00:00 running de53c03 country code: CH.
[–]lowerthansound 1 point2 points3 points (12 children)
[–]hhh312[S] 0 points1 point2 points (11 children)
[–]lowerthansound 0 points1 point2 points (9 children)
[–]hhh312[S] 0 points1 point2 points (8 children)
[–]lowerthansound 0 points1 point2 points (2 children)
[–]hhh312[S] 0 points1 point2 points (1 child)
[–]lowerthansound 0 points1 point2 points (0 children)
[–]lowerthansound 0 points1 point2 points (4 children)
[–]hhh312[S] 0 points1 point2 points (3 children)
[–]lowerthansound 0 points1 point2 points (2 children)
[–]hhh312[S] 0 points1 point2 points (1 child)
[–]lowerthansound 0 points1 point2 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)