嗨,我是python新手,我有一个问题要弄清楚我的代码出了什么问题,为什么单元测试失败了?下面是运行测试时的代码、单元测试和错误消息:
legs = []
stomach = []
class Centipede(object):
def __init__(self):
def __str__(self):
return ','.join(self.stomach)
def __call__(self,*args):
[self.stomach.append(arg) for arg in args]
#self.stomach.append(args)
def __repr__(self):
return ','.join(self.legs)
def __setattr__(self, key, value):
print("setting %s to %s" % (key, repr(value)))
if key in ([]):
self.legs.append(key)
#self.__dict__[key] = value
object.__setattr__(self, key,value)
单元测试代码
^{pr2}$运行测试时生成的错误消息:
AttributeError: 'Centipede' object has no attribute 'legs'
AttributeError: 'Centipede' object has no attribute 'stomach'
你在课堂外宣布了腿和胃-拉尔夫不知道它们应该属于他。在
把腿和胃放在
class
线之后,并将它们与__init__
相同的缩进量,应该会让你向前迈进。在把腿和胃移到蜈蚣身上。 (我一直想说:)
在自己的腿在你使用它之前是不会设置的。你确定你不只是指没有“自我”部分的“腿”,因为你有一个称为“腿”的全局变量,你没有使用?在
相关问题 更多 >
编程相关推荐