【发布时间】:2016-09-16 05:26:44
【问题描述】:
我的 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
问题是如何使用列表索引访问在 Shape 超类中创建的坐标列表? 是否有可能使用另一个列表对列表进行索引?
【问题讨论】:
-
可能不相关,但
p[0,0], p[0,1] == (1,1)评估为p[0,0], (p[0,1] == (1,1)) -
有可能是这样的:a = [1,1] assert a[0], a[1] == (1,1)
-
a[0], a[1] == (1,1)将评估为(1, False)。你可能想要assert (a[0], a[1] == (1,1))。相关:stackoverflow.com/q/37313471/1639625 -
你是对的!我没有打印答案来看看我得到了什么
-
p[1, 0]之类的会是什么?这似乎并不明显,我很好奇。
标签: python