-2

i have class hero

class Hero:
    def __init__(self):
        self.positive_effects = []
        self.negative_effects = []

        self.stats = {
            "HP": 128,
            "MP": 42,
        "SP": 100,

        "Strength": 15,
        "Perception": 4,
        "Endurance": 8,
        "Charisma": 2,
        "Intelligence": 3,
        "Agility": 8,
        "Luck": 1
    } 

def get_positive_effects(self):
    return self.positive_effects.copy()

def get_negative_effects(self):
    return self.negative_effects

def get_stats(self):
    return self.stats.copy()

i need to create decorator spell, for example:

class AbstractEffect(Hero):
    def __init__(self, base):
            self.base = base

    def get_stats(self):
            self.base.get_stats()   

    def get_positive_effects(self):
        self.base.get_positive_effects()

    def get_negative_effects(self):
        self.base.get_negative_effects()

class Berserk(AbstractEffect): 
    def __init__(self):
        self.positive_effects.append('Berserk')
        self.stats.HP += 50
        self.stats.Strength += 7
        self.stats.Endurance += 7
        self.stats.Agility += 7
        self.stats.Luck += 7
        self.stats.Intelligence -= 3
        self.stats.Perception -= 3
        self.stats.Charisma -= 3

but when i use it, i recive error

man = Hero()
man = Berserk(man)

TypeError Traceback (most recent call last) in () ----> 1 man = Berserk(man)

TypeError: __init__() takes exactly 1 argument (2 given)

What is wrong?

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • 1
    your berserk class __init__ method only takes in one parameter(self) but you are passing 2, self and man – Nullman Jan 17 '19 at 13:36
  • Where do you see a "decorator class" here ? or perhaps your definition of "decorator" is totally unrelated to Python's notion of decorator (cf https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators) ? If yes you should possibly edit your question's title. – bruno desthuilliers Jan 17 '19 at 13:50

1 Answers1

0

The reason that this error is occurring is because the constructor of Berserk does not accept an Object of the type Man. Therefore, what you must do is change the constructor's arguments, that way you can pass in Berserk(man).

class Berserk(AbstractEffect): 
    def __init__(self, man):
        #....
        self.stats.Charisma -= 3

Now, since Berserk(man) takes 2 arguments, you should not get the error. For more information about constructors, you can check out this StackOverflow question that may provide more insight.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23