所以這個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