#๐ Question about super().__init__() method initialisation
27 messages ยท Page 1 of 1 (latest)
@plucky axle
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.
do you have a specific example in mind?
as soon as you make a class inherit another, that subclass will get everything in the superclass, without having to call __init__, because __init__ is just what happens when an instance of the class is made
!e
class A:
def fn(self):
print('test')
class B(A):
pass
B().fn()
:white_check_mark: Your 3.12 eval job has completed with return code 0.
test
so super().init() doesn't have to be explicitly defined in order for a subclass to inherit the methods of a superclass?
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
so the inheritance of the superclass' methods are abstracted away in the init method?
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
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello from <class '__main__.Child'>. value='world'
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
ah okay so the init method is just for defining instance variables?
technically it can do other things, but the most common usage by far is that
okay but the idea is that it is used for instance object initialisation (thus the name init) and not inheritance
yes
thanks a bunch
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
wdym redefine? like overriding the parent methods?
!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()
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello from b
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
ah i see what you mean, thanks
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.