【发布时间】:2014-04-05 04:13:58
【问题描述】:
我是 python 新手,我很难理解为什么我在调用 main() 时不断收到“AttributeError:worker instance has no attribute 'workerNo'”。
beltLength = 5
class worker:
def __init__(self, wId):
workerNo = wId
def main():
production_Line = productionLine()
workers = []
for index in xrange(0, beltLength*2):
workers.append(worker(index))
print workers[index].workerNo
我的想法是它应该附加 10 个新的工人实例,其 workerNo 属性等于列表中的索引。 谢谢
【问题讨论】:
-
在
__init__:self.workerNo = wid- 在您的代码中缺少self.。常见错误。 -
旁白:由于您使用的是 2.7,因此您应该始终使您的类成为
object的子类,即class worker(object):。这将释放magical ponies。
标签: python python-2.7 attributeerror