So this Python problem has been giving me problems since I’ve tried refactoring the code into different files. I have a file called object.py and in it, the related code is:
class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it's always represented by a character on screen.
def __init__(self, x, y, char, color):
self.x = x
self.y = y
self.char = char
self.color = color
def move(self, dx, dy):
#move by the given amount, if the destination is not blocked
#if not map[self.x + dx][self.y + dy].blocked:
self.x += dx
self.y += dy
Now, when I try to compile this file specifically I get this error:
TypeError: unbound method __init__() must be called with Object instance as first argument (got int instance instead)
The code that is attempting to call this is:
player = object_info.Object.__init__(BurglaryConstants.SCREEN_WIDTH/2, BurglaryConstants.SCREEN_HEIGHT/2, '@', libtcod.white)
Which causes this error when compiling:
AttributeError: 'module' object has no attribute 'Object'
So what the heck is going on with all this and how should I be refactoring this? Also I assume having a class called Object isn’t a very good coding practice, correct?
Thanks for your help!
Update
You are defining
Objectin a file calledobject.py. And yet the client refers toobject_info.Object. Is this a typo?Correct. Rename your class to something else, say
GenericObjectorGenericBase. Also don’t use the module nameobject.py. Change it appropriately.Also
You are constructing an instance of
Objectbut the way you are doing it is wrong. Try this:This chapter from Dive Into Python should prove useful.