簡體   English   中英

TypeError: __init__() 缺少 2 個必需的位置 arguments: 'no_of_arrows' 和 'email'

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

我使用多個 inheritance 功能編寫了 python 3.9 代碼。 我使用 Anaconda Navigator 中的 Jupyter Notebook 來編寫和運行這段代碼。 它在向導 class init () 方法中給出了 TypeError:


# 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)

這是帶有 TypeError 的 output:


*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'*

請幫我找出錯誤所在。

當我注釋掉 super(). init (email) 向導 class init () 方法中,代碼運行沒有任何錯誤。

提前致謝。

我不確定你想在這里實現什么,但你必須記住所有__init__都會被HybridAttacker 你錯過了每個 function 的參數,所以你寧願使用**kwargs然后得到你需要的。

所以你的__init__函數可能看起來像

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

並且HybridAttacker可以為每個父母的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM