-1

So I'm trying to write a function to instantiate a class. Since I am looping through said function, I need a condition so the function only instantiates the class if it has not yet been done (otherwise HP would be maxed out every loop, among other problems). Something I tried:

class Fighter:
    def __init__(self):
        self.maxHP = 100
        self.HP = self.maxHP
        self.attack = 10

def before_fight():
    if fighterYOU == None:    #this is where I would like a condition that checks if fighterYOU is already instantiated. "None" does not work.
        fighterYOU = Fighter()
    fighterYOU.update()
    fight()

before_fight()
#...program continues

Is a check like this possible, or should I just try moving the instantiation somewhere else, where it won't be looped through?

Astudent
  • 186
  • 1
  • 2
  • 16

1 Answers1

1

If I understand correctly, you want to avoid recreating the fighter. For this, just set the variable to None first:

class Fighter:
    def __init__(self):
        self.maxHP = 100
        self.HP = self.maxHP
        self.attack = 10

def before_fight():
    if fighterYOU == None:    #this is where I would like a condition that checks if fighterYOU is already instantiated. "None" does not work.
        fighterYOU = Fighter()
    fighterYOU.update()
    fight()

fighterYOU = None   # default is None

before_fight()
#...program continues
Mike67
  • 11,175
  • 2
  • 7
  • 15