python - When I try to add an argument to one class I get a Typeerror for classes that inherint from another one -
I have received this code and there is no error in it:
class weapon ( Object): # No know that it can be successor (object) def (__int __ (self, weapon): self.weaponName = weaponName class character (object): def __init __ (self, name, weapon name): self .name = name self.weapon = weapon (weapon name) class NPC (character): def __init __ (self, name, weapon name): super () .__ init __ (name, weapon) class friendly (NPC): def __init __ (Self (Name, weapon name) sword = weapon ('sword') guard = npc ('bob', 'sword') print (guard.weapon.weaponName) print (): (name, weapon name): super () .__ init __ Guard.name) But when I try to add a loss feature in the weapon class:
class weapon (object): def __init __ (Self, weapon, arms, damages): self.weaponName = WeaponName self.weaponDamage = weaponDamage class character (object): def __init__ (self, name, weaponName): self.name = name self.weapon = weapon (weapon name) class NPC (Charit ): Def __init __ (self, name E, weaponName): super () .__ init __ (name, weapon name) class friendly (NPC): def __init __ (self, name, weapon name): super () .__ Init __ (name, weaponName) sword = weapon ('sword', 15) guard = npc ('bob', 'sword') print (guard.weapon.weaponName) print (guard.name) I am getting:
traceback (most recent call final): file "D: \ Python files \ legacy test.py", line 21, & lt; Module & gt; Guard = NPC ('Bob', 'Sword') file "D: \ Python Files \ Legacy test.py", line 13, __init__ in Super () .__ init __ (name, weaponName) file "D: \ Python Files \ Legacy " Why is that so? I do not want to refer to asphalt anywhere in character character, I want to use it in later work such as attacks and if I have many weapons I would like to be able to say that 'this example of friendship There are X weapons' and from there it shows that the loss of weapon is due to seeing the exemplary example of the weapon class.
You asked:
Why is that so?
You specified in the weapon .__ init __ () that it requires the weaponDamage as an argument. If you make an example of weapon anywhere, then you have to give value to the command command . You can correct the error by providing a default value for weapon command .__ init _ () . class weapon (object): def __init __ (self, weapon name, weapon = 0): self.weaponName = weaponName self.weaponDamage = weaponDamage
Comments
Post a Comment