RedIcon RedIcon - 7 months ago 17
Python Question

How to call constuctor from superclass

class User(object):

def __init__(self, id_code, name, surname):
self.id_code = id_code
self.name = name
self.surname = surname

class Worker(User):

def __init__(self, username, password, worker_type):
super(User, self).__init__()
self.username = username
self.password = password
self.worker_type = worker_type


How i can call constructor with attributes from super class?

ex:

id_code = "test_code"
name = "test_name"
surname = "test_surname"
username = "test_username"
password = "test_pass"


How i can solve it?

Answer

First you had a small logical mistake in there which @MartijnPieters already pointed out: The first argument of super should be the current class:

super(Worker, self).__init__()

In order to pass them to the superclass you need to accept these arguments in the subclass. Either by hardcoding:

class User(object):

    def __init__(self, id_code, name, surname):
        self.id_code = id_code
        self.name = name
        self.surname = surname

class Worker(User):

    def __init__(self, id_code, name, surname, username, password, worker_type): # changed
        super(Worker, self).__init__(id_code, name, surname) # changed
        self.username = username
        self.password = password
        self.worker_type = worker_type

or using *args and **kwargs.