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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.