我已被分配为我的Python课程的练习。目标是创建一些几何对象作为要通过某些断言评估的Shape对象的子类。访问列表的索引是列表
下面是代码:
class Shape(object):
__metaclass__ = ABCMeta
def __init__(self, coords):
super(Shape, self).__init__()
self._coords = list(map(int, coords))
def __getitem__(self, key):
return self._coords[key]
def move(self, distance):
self._coords = distance
class Point(Shape):
def __init__(self, coords):
super(Point,self).__init__(coords)
if __name__ == '__main__':
p = Point((0,0))
p.move((1,1))
assert p[0,0], p[0,1] == (1,1) # Check the coordinates
的问题是,我怎么能访问在外形超类创建了列表索引COORDS的名单? 列表是否有可能使用另一个列表进行索引?
可能不相关,但是'p [0,0],p [0,1] ==(1,1)'评估为'p [0,0],(p [0,1] ==(1 ,1))' –
有可能有这样的:a = [1,1] 断言[0],a [1] ==(1,1) – kotzimop
a [0],a [1] = =(1,1)'将评估为'(1,False)'。你可能想'assert(a [0],a [1] ==(1,1))'。相关:http://stackoverflow.com/q/37313471/1639625 –