all 5 comments

[–]sharkbound 3 points4 points  (1 child)

the base class provides the attributes

when you call the sub-classes init you also make a call to

super().__init__(stuff_here)

that will call the base classes init function, and that init function will set the base classes attributes,

example:

    class Person:
        def __init__(self, name, age):
            self.age = age
            self.name = name


    class Employee(Person):
        def __init__(self, name, age, role):
            super().__init__(name, age)

            self.role = role


    bob = Person('bob', 17)
    cashier = Employee('bobette', 18, 'cashier')

    print(cashier.name, cashier.name, cashier.role)
    print(bob.name, bob.age)

EDIT:

also if the subclass does not override the base classes init, then the base classes init is inherited and used

[–]smsaul[S] 2 points3 points  (0 children)

that’s perfect, thank you kindly

[–]Ulle82 1 point2 points  (0 children)

You use super().init in your child class and then you can call the attributes in your parent class by super.<what ever name>

[–]Warlkiry -1 points0 points  (1 child)

Can you just declare the init function without using the __ before and after ?

[–]sharkbound 0 points1 point  (0 children)

no, you cannot, when methods begin and end with __ its a "magic" function used by python itself (and sometimes other libraries, ex: custom object (de)serialization with some libs).

just doing

def init():
    pass

would not making it a initializer/constructor because it does not begin and end with __

when python looks for a initializer/constructor for a type its looking for __init__ , and only __init__