【发布时间】:2011-08-19 15:23:05
【问题描述】:
由于我是 stackoverflow 社区的新手,我不知道我是否应该将其作为一个新问题或我在这里提出的问题的延续 error with appending to a file and using an array 发布。
这里的代码基本相同,只是多了一行:
class component(object):
def __init__(self,
name = None,
height = None,
width = None):
self.name = name
self.height = height
self.width = width
class system(object):
def __init__(self,
name = None,
lines = None,
*component,
**kwargs):
self.name = kwargs.get('name')
self.component = component
self.lines = kwargs.get('lines') or []
def writeTOFile(self,*component):
self.component = component
line =" "
self.lines.append(line)
line= "#----------------------------------------- SYSTEM ---------------------------------------#"
self.lines.append(line)
line = "Width = %d" % component.width
self.lines.append(line)
def writeFile(self):
ef = open('file1.d', 'w')
ef.write('\n'.join(self.lines))
ef.close()
Component1 = component ( name = 'C1',
height = 500,
width = 400)
Component2 = component ( name = 'C2',
height = 600,
width = 700)
Component_list = [Component1, Component2]
system1 = system(Component_list)
system1.writeTOFile(Component_list)
system1.writeFile()
我添加的行是这样的:
line = "Width = %d" % component.width
self.lines.append(line)
我得到的错误是这样的:
Traceback (most recent call last):
File "C:\Python27\Work\trial2.py", line 55, in <module>
system1.writeTOFile(Component_list)
File "C:\Python27\Work\trial2.py", line 37, in writeTOFile
line = "Width = %d" % component.width
AttributeError: 'tuple' object has no attribute 'width'
类组件明明有一个叫width的属性,所以不明白为什么会出现这个错误。 我知道该组件是一个组件数组,所以这可能是原因......但我试图在(组件)范围内使用 for ,但显然我缺乏使其工作的技能。 提前致谢。
【问题讨论】: