开发者

TypeError: 'builtin_function_or_method' object is unsubscriptable

开发者 https://www.devze.com 2023-02-19 00:53 出处:网络
I\'m getting a strange error from the Python interpreter when I run this code: def make_map(): map = [[Tile(0, 0) for col in range(MAP_WIDTH)] for row in range(MAP_HEIGHT)]

I'm getting a strange error from the Python interpreter when I run this code:

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.

Thanks, Elliot Bonneville

EDIT: I forgot to include my Tile class code and the full traceback:

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:

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.

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. (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.

Of course Python has a map builtin function. Is the code you provided really literally what's in your code, or (e.g.) is map set up in one place and then used in another? 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.

Incidentally, because Python has that builtin, it's probably bad style to call one of your variables map in the first place.


You have a name clash and the build-in map() method is used here.


As others have guessed, the map in render_all refers to the global built-in function 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. 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.

Note that generally shouldn't shadow built-in names, i.e. don't name anything map or filter or any or ...

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
0

精彩评论

暂无评论...
验证码 换一张
取 消