Can someone explain to me how the funcion list.index() functions? I have the following code:
def getPos(self,tile):
print self.tiles[5][5]
print tile
try:
myIndex = self.tiles.index(tile)
#some code
except:
print "exception raised"
#some code
The result:
<Tile.Tile instance at 0x36BCEB8>
<Tile.Tile instance at 0x36BCEB8>
exception raised
Do you have an idea why list.index() returns an exception although the tile variable is a reference to an element of tiles[][] ? Thanks a lot.
ps: btw I'm passing tiles[5][5] in this specific case
except:- always be specific in what exceptions you want to catch, or you might catch some that you are not expecting.some_list.index(some_item)returnsi, thensome_list[i]should returnsome_item. What are you expectingmyIndexto be after yourindexcall? If it's going to work, you'd need to be able to sayself.tiles[myIndex]. But there's obviously no possible thing thatmyIndexcould be to make that work. Soself.tile.index(tile)can't be expected to work.