我越來越從Python解釋一個奇怪的錯誤,當我運行此代碼:類型錯誤:「builtin_function_or_method」對象是unsubscriptable
TypeError: 'builtin_function_or_method' object is unsubscriptable
的:
def make_map():
map = [[Tile(0, 0) for col in range(MAP_WIDTH)] for row in range(MAP_HEIGHT)]
for x in range(MAP_WIDTH):
for y in range(MAP_HEIGHT):
map[x][y].tileType = round((libtcod.noise_perlin(noise2d,[y/MAP_WIDTH,x/MAP_HEIGHT])*100), 0)
它在終端返回該回溯也指向這個功能:
def render_all():
global color_light_wall
global color_light_ground
#go through all tiles, and set their background color
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
tileType = map[x][y].tileType
if tileType>30:
libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET)
else:
libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET)
#draw all objects in the list
for object in objects:
object.draw()
#blit the contents of "con" to the root console
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
我認爲他們都與這條線有關:tileType = map[x][y].tileType
但是如果有人能夠闡明這一點,我會很感激。
感謝,埃利奧特博納維爾
編輯:我忘了,包括我的瓷磚類代碼和完全回溯:
class Tile:
#a tile of the map and its properties
def __init__(self, tileType, blocked):
self.tileType = tileType
self.blocked = blocked
回溯:
File "kindred.py", line 123, in <module>
render_all()
File "kindred.py", line 64, in render_all
tileType = map[x][y].tileType
TypeError: 'builtin_function_or_method' object is unsubscriptable
請提供*全*回溯,而不是回跡片段 - 它們是完全沒有幫助 – 2011-03-20 14:59:44
好了,對不起。編輯。 – 2011-03-20 15:00:13
好吧,顯然'map'包含「內置函數或方法」而不是'Tile'對象。我們無法通過您展示的代碼告訴您爲什麼或者發生了什麼。嘗試'print repr(map)',併發布輸出(或者輸出的一個片段,如果它很大的話)。 – delnan 2011-03-20 15:01:33