#π Syntax question/dunders
60 messages Β· Page 1 of 1 (latest)
@hazy marlin
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You know the easiest way to figure this out is to add a print statement before each line, and see the output
Do you know about inheritance
Where it says: #line3, change this to print("Line 3")
in case its hard to read
a lil bit
thats like using test cases right?
No, I'm just saying: Run the code.
And add print statements.
Iβm assuming if this is homework they will have to know how to do it without running code
This is a very important skill.
Agree... just better to teach a person to fish and all that.
But running code is great for learning
Yeah I agree
But after they add print statements I think we should explain why itβs that order
Iβve gtg tho actually
You get the answer?
i tried that and when i ran the code, i got Line 3 in the shell
Did you do the other lines too?
Show the code you ran
(my goal was to get them to see the answer via prints, then explain it)
no π my fault one sec
Orders for __init__ are no different to other functions/methods. You make a Child. That calls its init. The child init calls the parent init, and the parent init calls the grandparent. In exactly the order of the statements in each init method - there's no magic or anything. The only automatic part is the call to Child.__init__. The other calls are explicit in the code.
This is somewhat a trick question because where the line comments are placed.
Yah... so let's discuss
i still don't fully understand why it runs in that order though
It's a trick/tricky question because the line 2 is before the super, but line 3 is after the super
So what happens is: Child.__init__ is called first. Does that make sense, for starters?
so the def __init is called before the super init and line 3 is called after the super from line 2?
def __init__ declares the function __init__, it's not really "run", per se, when you call Child(). The function __init__ is called.
What's happening is: Child.__init__ is called when you construct Child(). The first step of Child.__init__ calls Parent.__init__. The first step of Parent.__init__ is "Line 2", so that prints first. Then, it calls GrandParent.__init__, whose first line is Line 1.
but why do we not begin at class GrandParent if its line 1?
It's possible that Neptune is thinking that all the parent and grandparent inits are somehow always called because of the class inheritance. They are not.
Python make an "empty" Child instance and calls its __init__ method to initialise it. That is all. The Child init happens to call the parent init, and the parent init happens to call the grandparent init. But only because those calls are in the code of their init methods.
You need to take a step back and understand what "inheritance" means.
And what a "class" is.
I'm sure your class has some notes, but here's a brief explanation: https://python.swaroopch.com/oop.html
I captured a little gif of the debugger in this code. it might help
i understand it now
I'd be very surprised. It took most of us a long time to really understand this stuff. I wasn't trying to be negative: I just meant that you need to take a step back and really get it.
(because we all struggled with it at first)
we use super().__init__ to call for the initializer of the parent and grandparent classes, and Line 2 is executed first because the Child class's __init__ calls the parent classes __init__ method , correct?
The actions go:
- python calls child init
- child init calls parent init (via
super()) - parent init does line 2
- parent init calls grandparent init (via
super()) - grandparent init does line 1
- return
- return
- child init does line 3
- return
Line 1 is executed second because the parent classes __init__ method calls the Grandparent class's __init__ method using the super() function and line 3 is executed last because the Child class's __init__ method continues to priont Line 3
yup yup
i got it
And why doesn't grandparent have a super().init?
Importantly: there's nothing automatic about calling the superclass inits - that's an explicit part of your implementation of the child init.
bc theres no need to call a method from a parent class using super(), theres no further parent class for the grandparent domain
Aye. technically the parent of GrandParent is object, but its init does nothing π
so unless they come up with a great-grandparent ancestor in the hierarchy thers no method to invoke from the grandparent class
thanks guys
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π Syntax question/dunders