Search code examples
pythonclassoopsuperattributeerror

AttributeError: Type Object 'Ned' has no Attribute 'attack'?


Here's my code:

Classes:

class Player(object):
"""Base class for the player"""
def __init__(self, name, armour, attack):
    self.name = name
    self.armour = armour
    self.attack = attack
class Ned(Player):
    """The main player"""
    def __init__(self):
        super(Ned, name="Ned", armour=10, attack=3).__init__()

Line Causing the Issue:

entities.Ned.attack += 3

When I run this, I get:

AttributeError: type object 'Ned' has no attribute 'attack'

So I don't understand what's going on here. I imported using import entities and then used entities.Ned..., so I am fairly sure it's not something to do with file loading. All indentations are correct (this was an answer of two AttributeErrors on this website) and I made sure everything was spelt correctly. Anyone know what might be going on? Any answer I find either doesn't work or is too specific to work in my case. Thanks.


Solution

  • The Player class __init__ function is not indented correctly. You should add another 4 spaces/\t to the block.

    You have a wrong super definition. This is a proper one:

    super(Ned, self).__init__(name="Ned", armour=10, attack=3)
    

    You must create a class object first to use it so you should call it as:

    ned_object = entities.Ned()
    ned_object.attack += 3