#๐Ÿ”’ Question about super().__init__() method initialisation

27 messages ยท Page 1 of 1 (latest)

plucky axle
#

Just curious, does calling super().init() in a subclass initialise the methods of the superclass? I understand it inherits the attributes but I'm not very sure where the superclass methods are initialised

rugged solsticeBOT
#

@plucky axle

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.

pastel bolt
#

!e

class A:
    def fn(self):
        print('test')

class B(A):
    pass

B().fn()
rugged solsticeBOT
plucky axle
modest crater
#

calling super().__init__() will call the parent class's init method, only - it will not cause anything unrelated to what you are doing inside of that method

All methods are inherited when the class is created, regardless of whenever you are initializing or nor

plucky axle
#

so the inheritance of the superclass' methods are abstracted away in the init method?

modest crater
#

no

#

inheritance itself has nothing to do with init

#

!e ```py
class Parent:
@classmethod
def method(cls, value):
print(f"Hello from {cls}. {value=}")

class Child(Parent):
pass

Child.method("world") # Inherited from Parent. Neither class is defining init, and you do not even have any "Child" instance, just the class itself

rugged solsticeBOT
modest crater
#

unless you are doing something like self.function = ... inside of __init__, which would be extremely cursed, __init__ has nothing to do with the inherited methods

plucky axle
#

ah okay so the init method is just for defining instance variables?

modest crater
plucky axle
#

okay but the idea is that it is used for instance object initialisation (thus the name init) and not inheritance

modest crater
#

yes

plucky axle
modest crater
#

when inheriting, you have to call the parent's init method (for it to initialize the variables the methods inherited from the parent will most likely require), but technically you could just re-define instead of calling super

or just not define init at all and let it directly call the parent's init

#

but the methods are already there, it's just a matter of having the attributes these methods need to work with

plucky axle
modest crater
#

!e ```py
class A:
def init(self, a):
self.a = a
def greet(self):
print(f'Hello from {self.a}')

class B(A):
def init(self):
self.a = 'b'

b = B()
b.greet()

rugged solsticeBOT
modest crater
#

A.__init__ is not called, but since B.__init__ defines self.a it does not needs to call A.__init__ for A.greet(b) to work

plucky axle
#

ah i see what you mean, thanks

rugged solsticeBOT
#
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.