0

For some unknown reason my program seems to be creating an instance of the character but not assigning an _id even though the _id even has a default in the __init__.
I'm not sure why this is and yes I'm new to python and trying to learn OOP and inheritance to make sure I fully understood the tutorials I had watched I decided to create a kind of complex program for this.

class Entity(object):
    def __init__(self, _id = None):
        self._id = _id
        self._pos = (0, 0)
        self._vel = (0, 0)

# --------------------------------------------------------------

class Character(Entity):
    def __init__(self, _id = None, _name = None):
        self._id, self._name = _id, _name
        self._entity = Entity.__init__(self._id)

# --------------------------------------------------------------

character = Character(0, 'End')

The error's I get is as follow's...

File "test.py", line 119, in <module>
    character = Character(0, 'End')
File "test.py", line 59, in __init__
    self._entity = Entity.__init__(self._id)
File "test.py", line 5, in __init__
    self._id = _id

AttributeError: 'int' object has no attribute '_id'

I think this means that when a new object of Character/Entity is created it cannot find, init or define the _id variable? This is probably a simple error and I'm doing something very wrong but thanks in advance.

1 Answer 1

1

You are initializing the super-class incorrectly. You must pass self in as well:

self._entity = Entity.__init__(self, self._id)

However, it is easier to use super() in most cases:

self._entity = super().__init__(self._id)

In both cases:

character = Character(0, 'End')
print(vars(character))
# {'_id': 0, '_name': 'End', '_pos': (0, 0), '_vel': (0, 0), '_entity': None}

Also, it is pointless to assign the return value of the init (always None) to self._entity. You can just make it

Entity.__init__(self, self._id)

or

super().__init__(self._id)
Sign up to request clarification or add additional context in comments.

4 Comments

Also, probably best to remove the assignment to self._entity. It's just going to be None anyway.
That helped alot thankyou however im still uncertain about something the whole self._entity var always being None that was an attempt to create a var that refers to the Entity object of that instance but since you said it's pointless I guess there is some built in way to refer to the inherited values of a instance of a class?
@End As you have already inherited from Entity class, it properties will be available in Character class using self. If you want to access _id from Entity in character class methods just write self._id.
@Avij ah i see thank's so the Character class has a _id variable but so does the Entity class so if i call self._id which one is called the one from Character or the one from Entity? would i instead call Entity._id or maybe self.Entity._id?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.