TypeError: __init__() missing 2 required positional arguments: 'no_of_arrows' and 'email'

Viewed 883

I wrote a python 3.9 code using the multiple inheritance feature. I used the Jupyter Notebook in Anaconda Navigator to write and run this code. It gives a TypeError in the Wizard class init() method:


# multiple inheritance

class User:
    def __init__(self, email):
        self.email = email

    def sign_in(self):
        print('Logged in.')

    def attack(self):
        print('User attack.')


class Wizard(User):
    def __init__(self, name, power, email):
        super().__init__(email)
        self.name = name
        self.power = power

    def attack(self):
        super().attack()
        print(f'Attacking with power of {self.power}.')


class Archer(User):
    def __init__(self, name, no_of_arrows, email):
        super().__init__(email)
        self.name = name
        self.no_of_arrows = no_of_arrows

    def attack(self):
        print(f'Attacking with arrows. Arrows left - {self.no_of_arrows}.')

    def check_arrows_count(self):
        print(f'{self.no_of_arrows} left.')

    def run(self):
        print('run')


class HybridAttacker(Wizard, Archer):
    def __init__(self, name, power, no_of_arrows, email):
        Wizard.__init__(self, name, power, email)
        Archer.__init__(self, name, no_of_arrows, email)


hybrid_attacker = HybridAttacker('Tom', 50, 20, 'tom@gmail.com')
print(hybrid_attacker)

Here is the output with the TypeError:


*TypeError Traceback (most recent call last) <ipython-input-11-4769f9f86581> in <module>
     45 
     46 
---> 47 hybrid_attacker = HybridAttacker('Tom', 50, 20, 'tom@gmail.com')
     48 print(hybrid_attacker)
<ipython-input-11-4769f9f86581> in __init__(self, name, power, no_of_arrows, email)
     41 class HybridAttacker(Wizard, Archer):
     42     def __init__(self, name, power, no_of_arrows, email):
---> 43         Wizard.__init__(self, name, power, email)
     44         Archer.__init__(self, name, no_of_arrows, email)
     45 
<ipython-input-11-4769f9f86581> in __init__(self, name, power, email)
     14 class Wizard(User):
     15     def __init__(self, name, power, email):
---> 16         super().__init__(email)
     17         self.name = name
     18         self.power = power
TypeError: __init__() missing 2 required positional arguments: 'no_of_arrows' and 'email'*

Please help me to find what the error is.

When I comment out super().init(email) in the Wizard class init() method, the code is running without any errors.

Thanks in advance.

1 Answers

I'm not certainly sure what you want to achieve here, but you must remember that all __init__ will be called there by HybridAttacker. You miss params for each of this function so you would need rather to use **kwargs and then just get you need.

So your __init__ functions could look like

class Wizard(User):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.name = kwargs.get('name')
        self.power = kwargs.get('power')

and HybridAttacker could have specified params for each parent's init:

class HybridAttacker(Wizard, Archer):
    def __init__(self, name, power, no_of_arrows, email):
        Wizard.__init__(self, name=name, power=power, email=email)
        Archer.__init__(self, name=name, no_of_arrows=no_of_arrows)
Related