所以这个Python问题一直给我带来问题,因为我已经尝试将代码重构为不同的文件。我有一个名为object.py文件,并在它的相关的代码是:Python - TypeError:unbound method
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
现在,当我尝试编译该文件具体我得到这个错误:
TypeError: unbound method __init__() must be called with Object instance as first argument (got int instance instead)
的代码,试图调用是这样的:
player = object_info.Object.__init__(BurglaryConstants.SCREEN_WIDTH/2, BurglaryConstants.SCREEN_HEIGHT/2, '@', libtcod.white)
编译时,这会导致这个错误:
AttributeError: 'module' object has no attribute 'Object'
那么,这一切到底是怎么回事,我应该如何重构呢?另外我假设有一个叫Object的类不是很好的编码习惯,对吗?
感谢您的帮助!
这不起作用,编译时我仍然得到相同的AttributeError。 – Brad 2010-08-24 17:13:42
@Brad:你能粘贴你得到的错误的堆栈跟踪吗? – 2010-08-24 17:19:19
@Brad:更新了答案。往上看。 – 2010-08-24 17:20:49