Python - TypeError: unbound method

So this Python issue was giving me problems, as I was trying to reorganize the code into different files. I have a file called object.py and there is related code in it:

class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it 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, I get this error:

TypeError: unbound method __init__() must be called with Object instance as first argument (got int instance instead)

Code that tries to call this:

player = object_info.Object.__init__(BurglaryConstants.SCREEN_WIDTH/2, BurglaryConstants.SCREEN_HEIGHT/2, '@', libtcod.white)

What causes this compilation error:

AttributeError: 'module' object has no attribute 'Object'

So what the hell is going on with all this and how should I reorganize it? I also assume that the class of the Object class is not a very good coding practice, right?

Thank you for your help!

+5
source share
2 answers

Update

Object object.py. object_info.Object. ?

, Object , ?

. , GenericObject GenericBase. object.py. .

Object, , , . :

player = object_info.Object(BurglaryConstants.SCREEN_WIDTH/2, BurglaryConstants.SCREEN_HEIGHT/2, '@', libtcod.white)

Dive Into Python .

+4

-, , object. ( , Python 3, )

-, __init__, , - , Object(x, y, char, color).

+1

All Articles