Python:对象不支持索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22463866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Object does not support indexing
提问by Jonathan Spirit
Yes, this question has been asked before. No, none of the answers I read could fix the problem I have.
是的,这个问题以前有人问过。不,我阅读的所有答案都无法解决我遇到的问题。
I'm trying to create a little Bounce game. I've created the bricks like this:
我正在尝试创建一个小弹跳游戏。我已经创建了这样的砖块:
def __init__(self,canvas):
self.canvas = canvas
self.brick1 = canvas.create_rectangle(0,0,50,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick2 = canvas.create_rectangle(50,0,100,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick3 = canvas.create_rectangle(100,0,150,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick4 = canvas.create_rectangle(150,0,200,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick5 = canvas.create_rectangle(200,0,250,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick6 = canvas.create_rectangle(250,0,300,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick7 = canvas.create_rectangle(300,0,350,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick8 = canvas.create_rectangle(350,0,400,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick9 = canvas.create_rectangle(400,0,450,20,fill=random_fill_colour(),outline=random_fill_colour())
self.brick10 = canvas.create_rectangle(450,0,500,20,fill=random_fill_colour(),outline=random_fill_colour())
self.bricksId = [self.brick1,self.brick2,self.brick3,self.brick4,self.brick5,self.brick6,self.brick7,self.brick8,self.brick9,self.brick10]
And I'm trying to reference the ID of bricksId[0]over here:
我正在尝试引用bricksId[0]此处的 ID :
self.hit_brick(pos,self.bricks.bricksId[0])
Earlier, in the __init__, I define bricks as bricks, which is defined as Brick(canvas). However, the error states:
早些时候,在 中__init__,我将砖定义为砖块,其定义为Brick(canvas)。但是,错误指出:
TypeError: 'Brick' object does not support indexing
In the answers to the other questions of this subject, I cannot find any that help me access bricks.bricksId[0].
在这个主题的其他问题的答案中,我找不到任何可以帮助我访问bricks.bricksId[0].
回答by bitsplit
In order for the Brickobject to be indexable, you must implement the methods:
为了使Brick对象可索引,您必须实现以下方法:
__getitem____setitem____delitem__
__getitem____setitem____delitem__
You don't need all of them, only the ones you use.
您不需要所有这些,只需要您使用的那些。
However, this seems like a case of self.bricksbeing a brick instead of a list of bricks. A list of bricks is indexable; however, a brick itself is not unless you implement the methods above.
然而,这似乎self.bricks是一个砖块而不是砖块列表的情况。砖块列表是可索引的;但是,除非您实现上述方法,否则砖本身不是。
Check thisfor reference.
检查此以供参考。
In order to be able to call self.bricks.bricksId[number]when I needed:
为了能够self.bricks.bricksId[number]在我需要时调用:
def __getitem__(self,index):
return self.bricks.bricksId[index]
def __setitem__(self,index,value):
self.bricks.bricksId[index] = value

