0

I've got 4 .py files that I'm using for a basic text based game. Unfortunately, I can't figure out why an attribute is not carrying all the way through like I'd expect.

This method of assigning enemies is fairly round-about, but it's all because of the rest of my game logic.

Here's my code:

characters.py

class Enemy():
    def __init__(self, name):
        self.hit = 0

enemies.py

from characters import Enemy

class Zombie(Enemy):
    def __init__(self):
        self.name = "zombie"

selectors.py

import enemies

def enemysel():
    return enemies.Zombie()

game.py

import selectors

test = selectors.enemysel()
print(test.hit)

When I run game.py, I just keep getting this error:

AttributeError: 'Zombie' object has no attribute 'hit'

What am I doing wrong?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
YangTegap
  • 381
  • 1
  • 11
  • 1
    You forgot to call `super().__init__()` in the subclass. – Barmar Dec 22 '20 at 21:57
  • 1
    Zombie defines its own `__init__()` function, overriding the one from the parent class. – John Gordon Dec 22 '20 at 21:57
  • 1
    There is no implicit call to a parent-class constructor in Python. If you override `__init__` it is your responsibility to call the parent-class `__init__`. Note, in a sense, you don't inherit instance attributes in Python. You inherit methods that may define instance attributes. – juanpa.arrivillaga Dec 22 '20 at 22:01

0 Answers0