#πŸ”’ Syntax question/dunders

60 messages Β· Page 1 of 1 (latest)

hazy marlin
#

Can someone help me with this question? I dont understand orders for init

fleet anchorBOT
#

@hazy marlin

Python help channel opened

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.

leaden pebble
#

You know the easiest way to figure this out is to add a print statement before each line, and see the output

plain cobalt
#

Do you know about inheritance

leaden pebble
hazy marlin
#

in case its hard to read

hazy marlin
hazy marlin
leaden pebble
#

And add print statements.

plain cobalt
#

I’m assuming if this is homework they will have to know how to do it without running code

leaden pebble
#

This is a very important skill.

leaden pebble
plain cobalt
#

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

surreal shore
hazy marlin
surreal shore
leaden pebble
#

(my goal was to get them to see the answer via prints, then explain it)

hazy marlin
hazy marlin
daring wing
# hazy marlin Can someone help me with this question? I dont understand orders for __init__

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.

leaden pebble
#

This is somewhat a trick question because where the line comments are placed.

hazy marlin
#

ahh i got line 2, line 1, line 3

leaden pebble
#

Yah... so let's discuss

hazy marlin
#

i still don't fully understand why it runs in that order though

leaden pebble
#

So what happens is: Child.__init__ is called first. Does that make sense, for starters?

hazy marlin
#

so the def __init is called before the super init and line 3 is called after the super from line 2?

leaden pebble
hazy marlin
#

what about for GrandParent?

#

since theres no super()__init__

leaden pebble
#

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.

hazy marlin
#

but why do we not begin at class GrandParent if its line 1?

daring wing
#

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.

leaden pebble
#

And what a "class" is.

surreal shore
leaden pebble
# hazy marlin 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)

hazy marlin
daring wing
#

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
hazy marlin
#

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

leaden pebble
daring wing
#

Importantly: there's nothing automatic about calling the superclass inits - that's an explicit part of your implementation of the child init.

hazy marlin
daring wing
#

Aye. technically the parent of GrandParent is object, but its init does nothing πŸ™‚

hazy marlin
#

so unless they come up with a great-grandparent ancestor in the hierarchy thers no method to invoke from the grandparent class

#

thanks guys

fleet anchorBOT
#
Python help channel closed

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