[英]TypeError: 'builtin_function_or_method' object is unsubscriptable
I'm getting a strange error from the Python interpreter when I run this code: 运行此代码时,我从Python解释器收到一个奇怪的错误:
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)
It's returning this in the terminal: 它在终端中返回:
TypeError: 'builtin_function_or_method' object is unsubscriptable
The traceback is also pointing to this function: 追溯也指向此函数:
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)
I think they're both related to this line: tileType = map[x][y].tileType
but if somebody could shed some light on this, I'd appreciate it. 我认为它们都与此行有关: tileType = map[x][y].tileType
但是如果有人可以对此有所了解,我将不胜感激。
Thanks, Elliot Bonneville 谢谢,艾略特·邦纳维尔
EDIT: I forgot to include my Tile class code and the full traceback: 编辑:我忘了包括我的Tile类代码和完整的回溯:
class Tile:
#a tile of the map and its properties
def __init__(self, tileType, blocked):
self.tileType = tileType
self.blocked = blocked
Traceback: 追溯:
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
This error mean that python try to get a variable name "map" in tileType = map[x][y].tileType
but he don't find it any where so it fetch the build in function map , which is unsubscriptable because it's a built-in function that explain the error message: 此错误意味着python尝试在tileType = map[x][y].tileType
获取变量名称“ map”,但他在任何地方都找不到它,因此它获取函数map中的内部版本 ,这是无法订阅的,因为它是解释错误消息的内置函数:
TypeError: 'builtin_function_or_method' object is unsubscriptable
What i will advise you is first to change your variable name from "map" to whatever to not shadow any built-in function, and second when you will change your variable name you should have a NameError
error because your variable isn't defined so you should fix that. 我会建议您首先将变量名从“ map”更改为不遮盖任何内置函数的内容,其次,当您更改变量名时,您应该遇到NameError
错误,因为未定义变量,因此你应该解决这个问题。
Hope i get it right and hope this help :) 希望我做对了,希望对您有所帮助:)
Your make_map function doesn't fail for me when I provide definitions for MAP_WIDTH
, MAP_HEIGHT
, noise2d
and libtcod.noise_perlin
. 当我提供MAP_WIDTH
, MAP_HEIGHT
, noise2d
和libtcod.noise_perlin
定义时,您的make_map函数对我来说不会失败。 (But -- though I'm sure this has nothing to do with the error you're getting -- you need to be accessing the array as map[y][x]
, not map[x][y]
since it's a list of rows, not of columns. (但是-尽管我确定这与您得到的错误无关-您需要以map[y][x]
而不是map[x][y]
形式访问数组,因为它是行列表,而不是列列表。
Of course Python has a map
builtin function. 当然,Python具有map
内置函数。 Is the code you provided really literally what's in your code, or (eg) is map
set up in one place and then used in another? 您提供的代码确实是字面上的含义吗?或者(例如)将map
设置在一处然后在另一处使用? Because if for some reason your map
is out of scope when you start trying to reference map[x][y].tileType
then you'll get the builtin function map
instead, which will produce errors of the sort you list. 因为如果由于某种原因您的map
在尝试引用map[x][y].tileType
时超出范围,那么您将获得内置函数map
,这将产生您列出的排序错误。
Incidentally, because Python has that builtin, it's probably bad style to call one of your variables map
in the first place. 顺便说一句, 因为 Python具有内置功能,所以首先调用一个变量map
可能是不好的样式。
您有一个名称冲突,此处使用了内置的map()方法。
As others have guessed, the map
in render_all
refers to the global built-in function map
. 正如其他人所猜测的那样, render_all
的map
是指全局内置函数map
。 The map = ...
part in make_map
merely creates a local variable, which dies when the function returns and isn't visible to any other function. make_map
的map = ...
部分仅创建一个局部变量,该局部变量在函数返回时死亡,其他任何函数均不可见。 Just return map
at the end of make_map
and store it somewhere render_all
can access it, or (even better) pass it as parameter to render_all
. 只需在make_map
的末尾return map
并将其存储在render_all
可以访问它的某个render_all
,或者(甚至更好)将其作为参数传递给render_all
。
Note that generally shouldn't shadow built-in names, ie don't name anything map
or filter
or any
or ... 请注意,通常不应遮盖内置名称,即不要为任何名称的map
或filter
或any
或...
Also, when iterating over a list or any other sequence (and you don't need to actually re-assign the objects stored in it), use: 另外,当遍历列表或任何其他序列时(并且您不需要实际重新分配存储在其中的对象),请使用:
for row in rows:
for obj in rows:
... # use obj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.