带有getitem的抽象类

时间:2018-10-15 13:49:56

标签: python-2.7

我已经创建了这个简单的脚本

from abc import ABCMeta, abstractmethod

class Shape(object):
    __metaclass__ = ABCMeta
    def __init__(self, coords):
        super(Shape, self).__init__()
        self._coords = list(map(list, [coords]))
        print self._coords

    def __getitem__(self, (x,y)):
        return self._coords[x][y]

class Point(Shape):
    def __init__(self,coords):
        super(Point, self).__init__(coords)



if __name__ == '__main__':

p = Point((0, 0))
print p._coords

assert p[0, 0], p[0, 1] == (0, 0)

print 'Success! Point tests passed!'

问题是断言失败。

Traceback (most recent call last):
 File "C:t4point.py", line 24, in <module>
  assert p[0, 0], p[0, 1] == (0, 0)
AssertionError: False

我怀疑getitem方法有问题。我的目标是更改代码以使所有内容都在main下运行(不更改main中的任何内容)

0 个答案:

没有答案